![](/img/trans.png)
[英]iOS: ReverseGeocoding Using CLGeocoder Vs. Google Geocoder API
[英]Using CLGeocoder instead of private API in map view
我正在使用google API獲取地理位置坐標。 到目前為止,我一直在使用的代碼如下
-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSStringEncodingConversionAllowLossy error:nil];
NSArray *items = [locationStr componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
//NSLog(@"items array %@", items);
if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[items objectAtIndex:2] doubleValue];
longitude = [[items objectAtIndex:3] doubleValue];
}
else {
//NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
我希望使用CLGeocoder
代替Google API從地址字符串中獲取位置坐標。
我一直在與CLGeocoder
合作,從在線示例中可以看到。 我發現使用CLGeocoder
有點困難。 我嘗試使用CLGeocoder
而不對以前的代碼進行太多更改,因此不必在現有代碼中進行重大更改。
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
self.placemarksArray = placemarks;
CLPlacemark *placeInfo = [placemarks objectAtIndex:0];
NSString *latitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.latitude ];
NSString *longitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.longitude ];
NSLog(@"latitudeString %@", latitudeString);
NSLog(@"longitudeString %@", longitudeString);
_latitudes = placeInfo.location.coordinate.latitude;
_longitudes = placeInfo.location.coordinate.longitude;
}];
location.latitude = _latitudes;
location.longitude = _longitudes;
return location;
但是發生的是,在執行該塊之前加載了地圖視圖。 在這種情況下,不會返回位置的確切值。
我的問題是:
CLGeocoder
,而無需對我之前的代碼進行太多更改。 還是我需要更改整個結構。 編輯:從viewDidLoad
調用上述方法
CLLocationCoordinate2D mapCenter = [self getLocationFromAddressString:fullAddress];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
[annotationPoint setCoordinate:mapCenter];
[annotationPointsArray addObject:annotationPoint];
addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName]autorelease];
[mapView addAnnotation:addAnnotation];
從getGeoLocation方法獲取CLLocationCoordinate2D
緯度和經度值后,將執行此操作。
CLGeocoder
異步發生(事實是在一個塊中完成了這一事實),因此_latitudes
和_longitudes
不會在方法本身返回時設置。 因此,很可能,是的,將需要對代碼進行一些輕微的重組。 我們很難建議,因為我們看不到調用此例程的代碼。 底線是,你必須重構的代碼,這樣,而不是返回的location
,你有completionHandler
的geocodeAddressString
調用任何你需要完成。
查看修改后的代碼,看來viewDidLoad
正在獲取位置坐標並創建注釋。 只需在地理編碼塊內添加注釋即可。
從而:
-(CLLocationCoordinate2D) geocodeAddressString:(NSString*)addressStr title:(NSString *)title subtitle:(NSString *)subtitle
{
CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
self.placemarksArray = placemarks;
CLPlacemark *placeInfo = [placemarks objectAtIndex:0];
id<MKAnnotation> annotation = [[[MyAddressAnnotation alloc] initWithCoordinate:placeInfo.location.coordinate
title:title
SubTitle:subtitle] autorelease];
[self.mapView addAnnotation:addAnnotation];
}];
}
然后, viewDidLoad
將按如下方式調用它:
[self geocodeAddressString:fullAddress title:firstName subtitle:lastName];
沒什么神奇的,但是您必須在塊內戳地圖視圖:
self.mapView.centerCoordinate = placeInfo.location.coordinate //or equivalent of this, like latitudes and longitudes change
如果要避免兩次繪制mapview,則需要攔截MKMapview的事件(尤其是mapViewWillStartLoadingMap ),在代碼中實現它們,並放置適當的標志來避免在未執行時間反向地理編碼塊之前繪制地圖。
試試這個答案。
延遲部分(即2秒)的調用方法。
[self performSelector:@selector(geoCodeUsingAddress:) withObject:userAddress afterDelay:2.0];
調用GetCurrentLocation函數
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)addressStr
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
self.placemarksArray = placemarks;
CLPlacemark *placeInfo = [placemarks objectAtIndex:0];
NSString *latitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.latitude ];
NSString *longitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.longitude ];
NSLog(@"latitudeString %@", latitudeString);
NSLog(@"longitudeString %@", longitudeString);
_latitudes = placeInfo.location.coordinate.latitude;
_longitudes = placeInfo.location.coordinate.longitude;
}];
location.latitude = _latitudes;
location.longitude = _longitudes;
return location;
}
希望會對您有所幫助。
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.