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.
CLLocationCoord开发者_Go百科inate2D 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 ?
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.
Here's a more modern approach.
- (CLLocationCoordinate2D) get2DCoordFromString:(NSString*)coordString
{
CLLocationCoordinate2D location;
NSArray *coordArray = [coordString componentsSeparatedByString: @","];
location.latitude = ((NSNumber *)coordArray[0]).doubleValue;
location.longitude = ((NSNumber *)coordArray[1]).doubleValue;
return location;
}
精彩评论