gpt4 book ai didi

iphone - 从自定义标注跳转到 MKMap 中的另一个标注

转载 作者:行者123 更新时间:2023-12-03 19:42:21 25 4
gpt4 key购买 nike

我一直在关注这个tutorial显示自定义注释标注气泡。如果您只有一个带有自定义注释 View 的注释,它会完美地工作。

但是,我的 map 上需要有更多内容,而且我在从自定义注释 View 切换到另一个注释 View 时遇到了麻烦。如果我在选择了一个图钉后单击另一个图钉并希望显示新的自定义注释 View ,则它不起作用。我首先要在 map View 上随机单击其他地方。我想我在 DidDeselect 方法中有一些工作要做,但我不确定......

遇到这样的问题你会如何解决?

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation && [view.annotation isKindOfClass:[MyHomeAnnotation class]]) {
[self.mapView removeAnnotation: self.calloutAnnotation];
}
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if ([view.annotation isKindOfClass:[MyHomeAnnotation class]]) {
if (self.calloutAnnotation == nil) {
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude
andLongitude:view.annotation.coordinate.longitude];
} else {
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
}
[self.mapView addAnnotation:self.calloutAnnotation];
self.selectedAnnotationView = view;
}
}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == self.calloutAnnotation) {
CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
if (!calloutMapAnnotationView) {
calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CalloutAnnotation"] autorelease];
calloutMapAnnotationView.contentHeight = 78.0f;
UIImage *asynchronyLogo = [UIImage imageNamed:@"cykelrød1.png"];
UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
[calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
}
calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
calloutMapAnnotationView.mapView = self.mapView;
return calloutMapAnnotationView;
} else if ([annotation isKindOfClass:[MyHomeAnnotation class]]) {
MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorGreen;
return annotationView;
}else if ([annotation isKindOfClass:[MyMapAnnotation class]]) {
MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"NormalAnnotation"] autorelease];
annotationView.canShowCallout = YES;
annotationView.pinColor = MKPinAnnotationColorPurple;
return annotationView;
}


return nil;
}

最佳答案

这是我的解决方案,我使用纬度和经度来区分应从 map View 中删除哪个标注。希望这会有所帮助。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"didSelectAnnotationView:%f", view.annotation.coordinate.latitude);

if (self.calloutAnnotation == nil) {

CalloutMapAnnotation *tempCallout = [[CalloutMapAnnotation alloc]
initWithLatitude:view.annotation.coordinate.latitude
andLongitude:view.annotation.coordinate.longitude];

self.calloutAnnotation = tempCallout;
[tempCallout release];

} else {

//remove callout when callout already exist
[self.myMapView removeAnnotation: self.calloutAnnotation];
self.selectedAnnotationView = nil;

//reposition
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;

}

[self.myMapView addAnnotation:self.calloutAnnotation];
self.selectedAnnotationView = view;
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {

NSLog(@"didDeselectAnnotationView:%f", view.annotation.coordinate.latitude);

//use the latitude and longitude to avoid remove twice

if (self.calloutAnnotation &&
self.selectedAnnotationView.annotation.coordinate.latitude == view.annotation.coordinate.latitude &&
self.selectedAnnotationView.annotation.coordinate.longitude == view.annotation.coordinate.longitude
) {

[self.myMapView removeAnnotation: self.calloutAnnotation];
self.selectedAnnotationView = nil;
self.calloutAnnotation.isAddtoMap = NO;
}

}

关于iphone - 从自定义标注跳转到 MKMap 中的另一个标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4657122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com