MKMapView 显示多个 MKPlacemark 的相同标题

发布于 2024-12-29 17:40:21 字数 1410 浏览 5 评论 0原文

我有一个 mkmapview,我在上面放置了几个地标图钉,但是我无法让图钉在标注上显示正确的标题,似乎随机显示地图上图钉集合中的标题。有什么想法吗?代码如下:

(void)viewDidLoad {
    [super viewDidLoad];

    [mapView setDelegate:self];

    CLLocationCoordinate2D geos = CLLocationCoordinate2DMake([putInLat doubleValue], [putInLong doubleValue]);
    aMarker = [[RNPlaceMark alloc] initWithCoordinate:geos Title:@"Location A"];

    CLLocationCoordinate2D geos2 = CLLocationCoordinate2DMake([takeOutLat doubleValue], [takeOutLong doubleValue]);
    bMarker = [[RNPlaceMark alloc] initWithCoordinate:geos2 Title:@"Location B"];

    NSArray *annots = [[NSArray alloc] initWithObjects:putInMarker, takeOutMarker, nil];
    [mapView addAnnotations:annots];

}

(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSString *title = annotation.title;
    MKPinAnnotationView *pinView=(MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:title];

    if(pinView==nil)
        pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:title] autorelease];

    if(annotation == aMarker)
        [pinView setPinColor:MKPinAnnotationColorGreen];
    else if(annotation == bMarker)
        [pinView setPinColor:MKPinAnnotationColorRed];

    pinView.canShowCallout=YES;
    pinView.animatesDrop=YES;


    return pinView;
}

I have a mkmapview that I'm dropping several placemark pins on, however I've not been able to get the pins to show the correct title on the callouts, seems to randomly show a title from the collection of pins on the map. Any ideas? Code looks like:

(void)viewDidLoad {
    [super viewDidLoad];

    [mapView setDelegate:self];

    CLLocationCoordinate2D geos = CLLocationCoordinate2DMake([putInLat doubleValue], [putInLong doubleValue]);
    aMarker = [[RNPlaceMark alloc] initWithCoordinate:geos Title:@"Location A"];

    CLLocationCoordinate2D geos2 = CLLocationCoordinate2DMake([takeOutLat doubleValue], [takeOutLong doubleValue]);
    bMarker = [[RNPlaceMark alloc] initWithCoordinate:geos2 Title:@"Location B"];

    NSArray *annots = [[NSArray alloc] initWithObjects:putInMarker, takeOutMarker, nil];
    [mapView addAnnotations:annots];

}

and

(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSString *title = annotation.title;
    MKPinAnnotationView *pinView=(MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:title];

    if(pinView==nil)
        pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:title] autorelease];

    if(annotation == aMarker)
        [pinView setPinColor:MKPinAnnotationColorGreen];
    else if(annotation == bMarker)
        [pinView setPinColor:MKPinAnnotationColorRed];

    pinView.canShowCallout=YES;
    pinView.animatesDrop=YES;


    return pinView;
}

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

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

发布评论

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

评论(1

猥琐帝 2025-01-05 17:40:21

我将代码切换为使用 MKPointAnnotation 它工作得很好,所以现在看起来像...

我在托管 UIMapVIew 的视图上的 viewDidLoad 方法中执行以下代码:

MKPointAnnotation *myMarker = [[MKPointAnnotation alloc] init];
[myMarker setTitle:@"Hello World"];
CLLocationCoordinate2D geos = CLLocationCoordinate2DMake([myMarkerLat doubleValue], [myMarkerLong doubleValue]);
[myMarker setCoordinate:geos];

NSArray *annots = [[NSArray alloc] initWithObjects:myMarker, nil];
[mapView addAnnotations:annots];

然后我...

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id
                                                                  <MKAnnotation>)annotation
{
    NSString *title = annotation.title;
    MKPinAnnotationView *pinView=(MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:title];

if(pinView==nil)
    pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:title] autorelease];

//If you want to change the color of the pin you can with something like...
//if(annotation == whatEverInstanceOfAMarkerIWantToKeep)
//    [pinView setPinColor:MKPinAnnotationColorGreen];

pinView.canShowCallout=YES;
pinView.animatesDrop=YES;


return pinView;

}

I switched the code to use MKPointAnnotation it worked fine, so now it looks like...

I'm executing the following code in my viewDidLoad method on the view that host the UIMapVIew:

MKPointAnnotation *myMarker = [[MKPointAnnotation alloc] init];
[myMarker setTitle:@"Hello World"];
CLLocationCoordinate2D geos = CLLocationCoordinate2DMake([myMarkerLat doubleValue], [myMarkerLong doubleValue]);
[myMarker setCoordinate:geos];

NSArray *annots = [[NSArray alloc] initWithObjects:myMarker, nil];
[mapView addAnnotations:annots];

then I have...

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id
                                                                  <MKAnnotation>)annotation
{
    NSString *title = annotation.title;
    MKPinAnnotationView *pinView=(MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:title];

if(pinView==nil)
    pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:title] autorelease];

//If you want to change the color of the pin you can with something like...
//if(annotation == whatEverInstanceOfAMarkerIWantToKeep)
//    [pinView setPinColor:MKPinAnnotationColorGreen];

pinView.canShowCallout=YES;
pinView.animatesDrop=YES;


return pinView;

}

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