我想给用户一个选项,让他可以在 iPhone 的谷歌地图中输入他想搜索的位置名称。当用户输入特定 map 的位置名称时。
现在我通过固定对象纬度和经度中坐标的值来做到这一点。
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=19.14;
location.longitude=73.10;
有没有办法在文本中给出这个坐标的值并将其转换为 CLLocationCoordinate2D 的值?
最佳答案
我不是很清楚你的问题,但如果你想将一个 NSString 转换成一个 CLLocationCoordinate2D 你可以使用以下方法:
{
[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
}
此代码不检查字符串的有效性,您可以检查从 componentsSeparatedByString 返回的 NSArray。
关于iphone - 如何将文本的 nsstring 值转换为 objective-c 中的 CLLocationCoordinate2D for google map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2131567/