iPad Mapkit - 更改“当前位置”的标题
在地图视图中,我显示当前用户位置。单击图钉后,其显示“当前位置”。我想将其更改为“我当前的位置”。我该如何改变它。 我还想更改计时器中当前用户位置图钉的颜色。比如每隔一秒它的颜色就会在绿色、紫色和红色之间变化。可以做吗?
我使用地图套件的显示默认位置,然后按如下方式操作注释图钉颜色:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您让地图视图显示用户位置的默认注释视图(蓝点),则实现起来会更简单(并且您会得到一个漂亮的蓝点,带有很酷的动画缩放圆圈)。
如果您必须使用图钉图像而不是蓝点来显示用户位置,则需要做更多的工作。
一、使用蓝点的简单方法:
如果您想对用户位置使用自定义注释视图,则应将图钉颜色更改代码放入自定义视图中。定期更改颜色的一种方法是使用 PerformSelector:withObject:afterDelay:。在 SolarAnnotationView.m 中,添加这两个方法:
同时将方法标头添加到 SolarAnnotationView.h 文件中。
然后像这样更改 viewForAnnotation 方法:
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:
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:
Also add the method headers to the SolarAnnotationView.h file.
Then change the viewForAnnotation method like this: