1

Is it possible to get coordinates by giving name of a place in iPhone? I don't want to use any Web Service as they have few limitations. Can i do it using iPhone SDK?

Thanks in advance.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Nir
  • 399
  • 3
  • 22

5 Answers5

0
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:YOUR_LOCATION completionHandler:^(NSArray* placemarks, NSError* error)     
{
    NSLog(@"completed");
    if ( error )
    {
        NSLog(@"error = %@", error );
    }
    else
    {
        //Here you get information about the place in placemarks array
    }
}];
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
Danial Hussain
  • 2,488
  • 18
  • 38
0

As @raj2raaz have mentioned, we can'nt get coordinates by just specify the name of the place, you must have to use web servics in iPhone to get coordinates.

http://iphonesdksnippets.com/post/2010/02/15/Get-Coordinates-from-Address.aspx

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

The short answer is no, you can't give an address and get the longitude / latitude position. Several people have written libraries that use the different web services, see this answer for details: Forward geocoding from the iPhone .

I know that you said you don't want to use various web services, but, well, you can't get everything for free. Somebody's CPU cycles are going to have to do a search. Most of them seem to me to have terms that are acceptable for most applications.

Community
  • 1
  • 1
Matthew Gillingham
  • 3,429
  • 1
  • 22
  • 26
  • You can if you include a database of places with the app. [GeoNames](http://download.geonames.org/export/dump/) provides that data. – Black Frog Apr 15 '11 at 10:29
  • You are right in a way. I still think the answer is no, in the sense that the question seemed as though it was asking for an API within the iPhone SDK. No such API exists. Your solution is not an API within the SDK but rather a somewhat large-scale project to store that data locally. There are some limitations, though. For example, you are limited to searching for what the Geonames database actually provides. So no street names, for example. It could also make the application download size large, unless the application has a targeted region. A web solution will be better in many cases. – Matthew Gillingham Apr 15 '11 at 13:13
  • Because the question said names, I am assuming names of cities. The Geonames file for cities with population over 15,000 people is a little over 22,000 cities. That is very small for Core Data. It can be smaller if he filters out counties he doesn't want before adding them to the project. Like you said, getting lat/long for an specific address is a lot more complicated. – Black Frog Apr 15 '11 at 13:38
0

Yes, it's possible, but you have to do some work. I am currently putting this effort in one of my project. The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge. I am adding their cites with population over 1,000 3.9M zip file to my project. It's contain long/lat of each city. I am going to parse each city into a custom NSObject and load them into Core Data.

In my project, instead of doing a city name search, I am going to find the closet city to a particular lat/long coordinate. Haversine formula is used to calculate the distance between two points on a sphere. Here are the formula written in Objective-C and Perl. My current progress of parsing that data can be found here. I still have work to complete.

Community
  • 1
  • 1
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • I suppose that this is technically "using the iPhone SDK" but I interpreted the question as asking for an API within the SDK to do geocoding. This seems a little bit different. But it might solve the problem, and will work very well if "the name of a place" is a fairly small list of possibilities. – Matthew Gillingham Apr 15 '11 at 13:18
-1

you can get the coordinates by calling this method .this method requires the address(name of place).

-(CLLocationCoordinate2D) addressLocation:(NSString *)addrss {
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [addrss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;

if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
    latitude = [[listItems objectAtIndex:2] doubleValue];
    longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
    //Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;

return location;

}

Gypsa
  • 11,230
  • 6
  • 44
  • 82