开发者

Web service for Location name using Latitude & Longitude

I have an iPhone application where i need to find out the current location(in terms of city name or probably the address). But i'm not been able to do that. I can find out the current latitude and longitu开发者_JAVA技巧de. Is there any web service(preferably free of cost) which can return me the current location name if i supply the latitude and longitude?


The feature you're looking for is called Reverse Geocoding.

Here is a list of web services that provide this function.

A couple of the most popular are Geonames.org, Google Maps Services API and Batchgeo.com for batch processing of multiple coordinates.


Assuming you are targeting iPhone OS 3.0 or higher, you can use MKReverseGeocoder which is part of the MapKit Framework. The Developer docs include a sample project. MKReverserGeocoder Class Reference


MKReverseGeocoder is a great class, but sometimes may return the error: Error Domain=PBRequesterErrorDomain Code=6001 "Operation could not be completed. (PBRequesterErrorDomain error 6001.)

luvieere's suggestion above are a great place to start, which is what I did as an example using the Google HTTP reverse geocoder. Here's my implementation of a GoogleReverseGeocoder class I wrote. The full explanation of how to use it can be found HERE.

// Send Google A Request
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false",self.locationToGeocode.coordinate.latitude,self.locationToGeocode.coordinate.longitude];
NSURL *urlFromString = [NSURL URLWithString:urlString];
NSStringEncoding encodingType = NSUTF8StringEncoding;
NSString *reverseGeoString = [NSString stringWithContentsOfURL:urlFromString encoding:encodingType error:nil];

// Parse Out Response
NSArray *listItems = [reverseGeoString componentsSeparatedByString:@","];
NSArray *tempAddressArray = [[listItems objectAtIndex:2] componentsSeparatedByString:@"\""];
NSArray *tempCountryArray = [[listItems objectAtIndex:[listItems count]-1] componentsSeparatedByString:@"\""];

// Did Google Find Address? 200 is yes
if ([[listItems objectAtIndex:0] intValue] == 200)
{
    // Set Class Member Variables
    [self setGoogleReturnDidFindAddress:YES];
    [self setGoogleReturnAddress:[tempAddressArray objectAtIndex:[tempAddressArray count]-1]];
    [self setGoogleReturnCountry:[tempCountryArray objectAtIndex:0]];
    [self setGoogleReturnCode:[[listItems objectAtIndex:0] intValue]];
    [self setGoogleReturnAccuracy:[[listItems objectAtIndex:1] intValue]];

} else if ([[listItems objectAtIndex:0] intValue] > 600)
{
    [self setGoogleReturnDidFindAddress:NO];
}


Wanna do it your own?

Most of these services you'll have to pay for with a thousand of limitations. Maybe you can have your own list of states and cities, alongside their known latitude and longitude.

Do not try to build the list yourself. This project for example is a Brazilian database of states and cities:

https://github.com/kelvins/Municipios-Brasileiros

A list of 5.570 cities is small as 430KBytes! Totally worth it considering the amount of data you have to store and the benefit you get from such.

So the best approach is to find a similar project from your country and make it happen.

So now that I've got my own list of cities, I can get a user the 5 nearest cities or do anything else I want to. With this simple query to my application MySQL database! Look how simple yet powerful

set @lat=-20.0080509185791;
set @lng=-44.003021240234375;

-- The main SQL query that returns the closest 5 airports.
SELECT id, nome, lat, lng, 111.045 * DEGREES(ACOS(COS(RADIANS(@lat))
 * COS(RADIANS(lat))
 * COS(RADIANS(lng) - RADIANS(@lng))
 + SIN(RADIANS(@lat))
 * SIN(RADIANS(lat))))
 AS distance_in_km
FROM cities
ORDER BY distance_in_km ASC
LIMIT 0,5;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜