在 map View 中,我显示当前用户位置。单击图钉上显示的“当前位置”。我想将其更改为“我的当前位置”。我怎样才能改变它。
我还想在计时器中更改当前用户位置图钉颜色。像每一秒它都应该在绿色、紫色和红色之间改变颜色。有可能做到吗?
我正在使用 map 套件的显示默认位置,然后操作注释图钉颜色如下:
- (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;
}
最佳答案
如果您让 map View 显示用户位置(蓝点)的默认注释 View ,则实现起来会更简单(并且您会得到一个漂亮的蓝点,带有很酷的动画缩放圆圈)。
如果您必须使用图钉图像而不是蓝点来显示用户位置,则需要做更多工作。
首先,使用蓝点的简单方法:
- (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;
}
如果您想对用户位置使用自定义注释 View ,则应将引脚颜色更改代码放在自定义 View 中。定期更改颜色的一种方法是使用 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 - 更改标题 "Current Location",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4324744/