iPad Mapkit - 更改“当前位置”的标题
Posted
技术标签:
【中文标题】iPad Mapkit - 更改“当前位置”的标题【英文标题】:iPad Mapkit - Change title of "Current Location" 【发布时间】:2010-12-01 13:20:36 【问题描述】:在地图视图中,我正在显示当前用户位置。单击图钉显示“当前位置”。我想将其更改为“我的当前位置”。我怎样才能改变它。 我还想在计时器中更改当前用户位置引脚颜色。就像每一秒它应该在绿色、紫色和红色之间改变它的颜色。有可能做到吗?
我正在使用地图工具包的显示默认位置,然后如下操作注释图钉颜色:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin
annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation];
annotationView.delegate = self;
[annotationView setPinColor:MKPinAnnotationColorGreen];
else
annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation];
annotationView.delegate = self;
return annotationView;
- (BOOL) CLLocationCoordinate2DEquals:(const CLLocationCoordinate2D)lhs withSecondCoordinate:(const CLLocationCoordinate2D) rhs
const CLLocationDegrees DELTA = 0.001;
return fabs(lhs.latitude - rhs.latitude) <= DELTA && fabs(lhs.longitude - rhs.longitude) <= DELTA;
【问题讨论】:
显示当前位置的显示方式。您是在使用地图视图的 showsUserLocation 属性来获取默认蓝点还是创建自定义图钉?展示如何添加注释和 viewForAnnotation 方法。 【参考方案1】:如果您让地图视图显示用户位置的默认注释视图(蓝点),这更容易实现(并且您会得到一个漂亮的蓝点和很酷的动画缩放圆圈)。
如果您必须使用 pin 图像而不是蓝点来显示用户位置,则需要做更多的工作。
首先,使用蓝点的简单方法:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
if ([annotation isKindOfClass:[MKUserLocation class]])
((MKUserLocation *)annotation).title = @"My Current Location";
return nil; //return nil to use default blue dot view
//Your existing code for viewForAnnotation here (with some corrections)...
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease];
//added autorelease above to avoid memory leak
annotationView.delegate = self;
//update annotation in view in case we are re-using a view
annotationView.annotation = annotation;
return annotationView;
如果您想将自定义注释视图用于用户位置,则应将引脚颜色更改代码放在自定义视图中。定期更改颜色的一种方法是使用 performSelector:withObject:afterDelay:。在 SolarAnnotationView.m 中,添加这两个方法:
-(void)startChangingPinColor
switch (self.pinColor)
case MKPinAnnotationColorRed:
self.pinColor = MKPinAnnotationColorGreen;
break;
case MKPinAnnotationColorGreen:
self.pinColor = MKPinAnnotationColorPurple;
break;
default:
self.pinColor = MKPinAnnotationColorRed;
break;
[self performSelector:@selector(startChangingPinColor) withObject:nil afterDelay:1.0];
-(void)stopChangingPinColor
[NSObject cancelPreviousPerformRequestsWithTarget:self];
还将方法头添加到 SolarAnnotationView.h 文件中。
然后像这样改变 viewForAnnotation 方法:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease];
annotationView.delegate = self;
//Update annotation in view in case we are re-using a view...
annotationView.annotation = annotation;
//Stop pin color changing in case we are re-using a view that has it on
//and this annotation is not user location...
[annotationView stopChangingPinColor];
if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin
[annotationView setPinColor:MKPinAnnotationColorGreen];
annotationView.canShowCallout = YES;
((MKPointAnnotation *)annotation).title = @"My Current Location";
[annotationView startChangingPinColor];
return annotationView;
【讨论】:
以上是关于iPad Mapkit - 更改“当前位置”的标题的主要内容,如果未能解决你的问题,请参考以下文章
每次我从当前位置滚动时,MapKit 都会自动缩放回当前位置