Objective-c – how to convert the nsstring value of text into CLLocationCoordinate2D in objective-c for google map

google mapsiphoneobjective c

i want to give option to user to put the location name what he want to search in google map from iphone. when user put the location name that particular map should come.

for now i am doing this by fixing the value of the of coordiante in its object latitude and longitude.

CLLocationCoordinate2D location=mapView.userLocation.coordinate;

location.latitude=19.14;
location.longitude=73.10;

is there any way to give the value of this coordinate in text and convert this to the value of CLLocationCoordinate2D ?

Best Answer

I am not entirely clear on your question, but if you are wanting to convert an NSString into a CLLocationCoordinate2D you can use the following:

{
    [self useLocationString:@"19.14,73.10"];
}

- (void) useLocationString:(NSString*)loc
{
    // the location object that we want to initialize based on the string
    CLLocationCoordinate2D location;

    // split the string by comma
    NSArray * locationArray = [loc componentsSeparatedByString: @","];        

    // set our latitude and longitude based on the two chunks in the string
    location.latitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:0] doubleValue]] autorelease];
    location.longitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:1] doubleValue]] autorelease];

   // do something with the location
}

This code doesn't check the validity of the string, which you could do be checking the NSArray you get back from componentsSeparatedByString.