MKReverseGeocoder 设置哪个注释的副标题

发布于 2024-10-20 09:10:25 字数 1893 浏览 0 评论 0原文

我正在使用 MKReverseGeocoder 对纬度/经度地址进行反向地理编码。但是,我无法确定要设置哪个注释的副标题。为了稍微扩展一下,我在每个注释上调用反向地理编码来确定地址,然后我想将它们的字幕文本设置为其地址。我已经成功地为第一个注释做到了这一点,但我不确定如何为第二个注释做到这一点。

这是我的代码:

- (void)loadAnnotations {
Annotation *annotation1 = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(a,b)];
annotation1.title = @"Anno1n";
CLLocation *annoe = [[CLLocation alloc] initWithLatitude:annotation1.coordinate.latitude longitude:annotation1.coordinate.longitude];
CLLocation *usrloc = [[CLLocation alloc] initWithLatitude:mapView.userLocation.coordinate.latitude longitude:mapView.userLocation.coordinate.longitude];
NSLog(@"%.2f km", [annoe distanceFromLocation:usrloc]/1000);
MKReverseGeocoder *geo = [[MKReverseGeocoder alloc] initWithCoordinate:annotation1.coordinate];
[geo setDelegate:self];
[geo start];
[mapView addAnnotation:annotation1];

Annotation *annotation2 = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(c,d)];
annotation2.title = @"Anno2";
geo = [[MKReverseGeocoder alloc] initWithCoordinate:annotation2.coordinate];
[geo start];
[mapView addAnnotation:annotation2];

}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSArray *array = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"];
if (geocoder.coordinate.latitude == mapView.userLocation.coordinate.latitude && geocoder.coordinate.longitude) {
    mapView.userLocation.title = @"Current Location";
    mapView.userLocation.subtitle = [NSString stringWithFormat:@"%@ %@", [array objectAtIndex:0], [array objectAtIndex:1]];
}
else {
    Annotation *annot = (Annotation *)[[mapView annotations] objectAtIndex:1];
    [annot setSubtitle:[NSString stringWithFormat:@"%@ %@", [array objectAtIndex:0], [array objectAtIndex:1]]];
    //NSLog(@"%@", [placemark addressDictionary]);
}
}

请让我知道如何做到这一点。

I am using MKReverseGeocoder to reversely geocode a latitude/longitude address. However I am having trouble determining which Annotation's subtitle is going to be set. To expand on that a bit, I am calling a reverse geocode on each annotation to determine the address, and then I would like to set their subtitle text to their address. I have managed to do it for the first annotation, but I am unsure on how I would do it for the second annotation.

Here is my code:

- (void)loadAnnotations {
Annotation *annotation1 = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(a,b)];
annotation1.title = @"Anno1n";
CLLocation *annoe = [[CLLocation alloc] initWithLatitude:annotation1.coordinate.latitude longitude:annotation1.coordinate.longitude];
CLLocation *usrloc = [[CLLocation alloc] initWithLatitude:mapView.userLocation.coordinate.latitude longitude:mapView.userLocation.coordinate.longitude];
NSLog(@"%.2f km", [annoe distanceFromLocation:usrloc]/1000);
MKReverseGeocoder *geo = [[MKReverseGeocoder alloc] initWithCoordinate:annotation1.coordinate];
[geo setDelegate:self];
[geo start];
[mapView addAnnotation:annotation1];

Annotation *annotation2 = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(c,d)];
annotation2.title = @"Anno2";
geo = [[MKReverseGeocoder alloc] initWithCoordinate:annotation2.coordinate];
[geo start];
[mapView addAnnotation:annotation2];

}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSArray *array = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"];
if (geocoder.coordinate.latitude == mapView.userLocation.coordinate.latitude && geocoder.coordinate.longitude) {
    mapView.userLocation.title = @"Current Location";
    mapView.userLocation.subtitle = [NSString stringWithFormat:@"%@ %@", [array objectAtIndex:0], [array objectAtIndex:1]];
}
else {
    Annotation *annot = (Annotation *)[[mapView annotations] objectAtIndex:1];
    [annot setSubtitle:[NSString stringWithFormat:@"%@ %@", [array objectAtIndex:0], [array objectAtIndex:1]]];
    //NSLog(@"%@", [placemark addressDictionary]);
}
}

please let me know how I can do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-10-27 09:10:25

不建议循环遍历您的注释并对每一项进行反向地理编码。反向地理编码服务有使用限制。 Apple 建议开发人员仅在由用户操作(例如选择注释)触发时调用反向地理编码服务。

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/Reference/Reference.html%23//apple_ref/occ/cl/MKReverseGeocoder

Looping through your annotations and reverse geocoding each one is not recommended. There's a usage limit for the reverse geocoding service. Apple recommends developers should only call the reverse geocoding service when triggered by user action, like selecting the annotation.

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/Reference/Reference.html%23//apple_ref/occ/cl/MKReverseGeocoder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文