iPad Mapkit - 更改“当前位置”的标题 [英] iPad Mapkit - Change title of "Current Location"

查看:106
本文介绍了iPad Mapkit - 更改“当前位置”的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In map view, i'm showing current user location. On click on the pin its showing "Current Location". I want to change it to "My Current Location". How can I change it. Also I want to change the current user location pin color in a timer. Some thing like every one second it should change its color between green, purple and red. Possible to do it?

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

I'm using map kit's show default location and then manipulating the annotation pin color as below:

- (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;

}

推荐答案

如果你让地图视图显示用户位置(蓝点)的默认注释视图,这可以更容易实现(并且你会得到一个带有很酷的动画缩放圆圈的漂亮蓝点)。

If you let the map view show the default annotation view for the user location (blue dot), this is simpler to implement (and you get a nice blue dot with cool animated zooming circle).

如果你必须使用图钉图像而不是蓝点来显示用户位置,那么还需要做一些工作。

If you must show the user location using a pin image instead of a blue dot, then some more work is needed.

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

First, the simple way using the blue dot:

- (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中,添加以下两种方法:


If you want to use your custom annotation view for the user location instead, you should put the pin color changing code in the custom view. One way to periodically change the color is using performSelector:withObject:afterDelay:. In the SolarAnnotationView.m, add these two methods:

-(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文件中。

Also add the method headers to the SolarAnnotationView.h file.

然后更改viewForAnnotation方法,如下所示:

Then change the viewForAnnotation method like this:

- (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 - 更改“当前位置”的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆