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

发布于 2024-10-05 08:51:57 字数 1374 浏览 2 评论 0原文

在地图视图中,我显示当前用户位置。单击图钉后,其显示“当前位置”。我想将其更改为“我当前的位置”。我该如何改变它。 我还想更改计时器中当前用户位置图钉的颜色。比如每隔一秒它的颜色就会在绿色、紫色和红色之间变化。可以做吗?

我使用地图套件的显示默认位置,然后按如下方式操作注释图钉颜色:

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

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-10-12 08:52:02

如果您让地图视图显示用户位置的默认注释视图(蓝点),则实现起来会更简单(并且您会得到一个漂亮的蓝点,带有很酷的动画缩放圆圈)。

如果您必须使用图钉图像而不是蓝点来显示用户位置,则需要做更多的工作。

一、使用蓝点的简单方法:

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

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

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];
}

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文