iPad Mapkit - 更改"当前位置"的标题

Sat*_*yam 7 mapkit ipad

在地图视图中,我显示当前用户位置.点击它显示"当前位置"的图钉.我想将其更改为"我的当前位置".我怎样才能改变它.另外,我想在计时器中更改当前用户位置的引脚颜色.有点像每一秒它应该改变绿色,紫色和红色之间的颜色.可以这样做吗?

我正在使用地图套件显示默认位置,然后操纵注释引脚颜色如下:

- (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;
Run Code Online (Sandbox Code Playgroud)

}

- (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;
Run Code Online (Sandbox Code Playgroud)

}

小智 20

如果让地图视图显示用户位置(蓝点)的默认注释视图,则可以更简单地实现(并且您可以获得带有炫酷动画缩放圆圈的漂亮蓝点).

如果必须使用图钉图像而不是蓝点显示用户位置,则需要进行更多工作.

首先,使用蓝点的简单方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)


如果要将自定义注释视图用于用户位置,则应将引脚颜色更改代码放在自定义视图中.定期更改颜色的一种方法是使用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];
}
Run Code Online (Sandbox Code Playgroud)

还要将方法标题添加到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;
}
Run Code Online (Sandbox Code Playgroud)