【发布时间】:2017-05-30 10:41:09
【问题描述】:
代码在这里。 它基本上可以工作,但我迷失了方向,无法弄清楚如何从 [geocoder] 例程中获取“point.title”。 point.title 是 Mapkit 注释的一部分。我可以按照注释掉,在 point.title 中放入一个简单的字符串,但我不能让它返回完整的 'locatedAT' 或任何 placemark.subLocality,例如。项目。
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
//point.title = @"Where am I?";
//point.subtitle = @"I'm here!!!";
////////////////////////
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
[geocoder reverseGeocodeLocation: loc
completionHandler:^(NSArray* placemarks, NSError* error){
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if (placemark) {
// NSLog(@"placemark %@",placemark);
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// NSLog(@"addressDictionary %@", placemark.addressDictionary);
// NSLog(@"placemark %@",placemark.region);
// NSLog(@"placemark %@",placemark.country); // Give Country Name
// NSLog(@"placemark %@",placemark.locality); // Extract the city name
// NSLog(@"location %@",placemark.name);
// NSLog(@"location %@",placemark.ocean);
// NSLog(@"location %@",placemark.postalCode);
// NSLog(@"location %@",placemark.subLocality);
//Print the location to console
// NSLog(@"I am currently at %@",locatedAt);
point.title = locatedAt;
}
else {
NSLog(@"Could not locate");
}
}
];
///////////////////////////
NSLog(@"point.title %@",point.title);
[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES]; }
【问题讨论】:
-
您能告诉我您遇到了什么错误或问题吗?
-
你想从反向地理编码中得到什么。
-
我正在尝试将locatedAt 字符串放入point.title 或point.subtitle 变量中。它是在地理编码器例程中生成的。
-
谢谢。标记被添加好了,放置图钉,但是 point.title 没有从地理编码器例程中添加。所以我有标记,但没有来自 reversegeocode 例程的标题?
-
Mahesh - 感谢您之前的回答。它运作良好。但是..现在好像已经被删除了?
标签: ios mapkit reverse-geocoding