如何从 iPhone 中的地址中查找经纬度
Posted
技术标签:
【中文标题】如何从 iPhone 中的地址中查找经纬度【英文标题】:How to find Latitude and Longitude from Address in iPhone 【发布时间】:2011-07-06 11:57:24 【问题描述】:我正在制作一个我正在获取地址的应用程序,我需要从该地址中找到纬度和经度并在地图中显示该地点
- (void)viewDidLoad
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation = YES;
MKCoordinateRegion region = 0.0, 0.0 , 0.0, 0.0 ;
geoCoder.delegate = self;
[geoCoder start];
double latitude = 0, longitude = 0;
region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"BarRestaurant";
ann.subtitle = @"...";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
else
[mapView.userLocation setTitle:@"I am here"];
return pinView;
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
appRestaurantAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
double latitude = 0, longitude = 0;
address = delegate.streetName;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result)
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil])
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil])
[scanner scanDouble:&longitude];
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
但是我无法找到纬度和经度,请帮助我在代理中做错的地方我从网络服务器获取地址
【问题讨论】:
【参考方案1】:试试这个代码,
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result)
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil])
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil])
[scanner scanDouble:&longitude];
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
也可以参考这个link
【讨论】:
感谢我实施的答案及其工作,但是在扫描纬度位置时,它不会进入 if 语句。 看来他们一定改变了格式;@"\"lat\":"
应该(目前)是 @"\"lat\" :"
,否则扫描仪将找不到它。 'lng' 方面也是如此。【参考方案2】:
ios 5.0 之后,google maps 和 google maps api 不应该在原生 mapview 中使用。这将违反谷歌和苹果的政策。
但我们可以从苹果提供的 mapkit 框架本身中找到位置。
请参阅以下链接。 https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html
使用“CLGeocoder”非常简单:
geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"1 Infinite Loop"
completionHandler:^(NSArray* placemarks, NSError* error)
for (CLPlacemark* aPlacemark in placemarks)
// Process the placemark.
];
【讨论】:
以上是关于如何从 iPhone 中的地址中查找经纬度的主要内容,如果未能解决你的问题,请参考以下文章