views:

546

answers:

1

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 ?

A: 

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.

Cannonade
A mismatch of languages there I think; should be `[self useLocationString:@"19.14,73.10"]`
dreamlax
@dreamlax Thanks ;) It is ridiculous how I often I do that.
Cannonade
You also put == instead of = in your last line ;)
Geraud.ch
@Geraud.ch Thanks. Fixed now :)
Cannonade