如何增加距离到MKAnotnotation的引脚? [英] How to add distance to pin of an MKAnnotation?

查看:87
本文介绍了如何增加距离到MKAnotnotation的引脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在地图视图上绘制mkannotations的应用程序(我希望我的术语正确,这有点令人困惑).

Ive got an app that plots mkannotations (i hope i get my terminology right...its kinda confusing) on a mapview.

当您点击它们时,我已经包含了该字幕.

I have already included the subtitle for when you tap on them.

我一直在网上寻找一种在这些标注中包括距离的方法,但目前还没有.我遇到了两个部分解决方案,我想知道是否应该将它们组合在一起.

I have been looking online for a way to include the distance in those callouts but im not quite there yet. I ran across two partial solutions and Im wondering if they should be combined.

  1. 首先,我没有将CoreLocation添加到我的项目中,我需要它吗?要不断更新我的用户位置并能够计算到每个点的距离?还是Mapkit会以某种方式包含我可以使用的用户位置数据?

  1. First, I didnt have CoreLocation added to my project, I need it right? To be constantly updating my user location and be able to calculate the distances to each point? Or does Mapkit somehow include a user location data that I can use?

部分解决方案A使用以下代码:

Partial Solution A uses this code:

`-((void)locationManager:(CLLocationManager *)manager

`-(void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

       fromLocation:(CLLocation *)oldLocation {

if(!newLocation) return;

if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&

    (oldLocation.coordinate.longitude != newLocation.coordinate.longitude)){

    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];

    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];

    CLLocationDistance distance = ([loc2 distanceFromLocation:loc1]) * 0.000621371192;

    //distance = distance;

    NSLog(@"Total Distance %f in miles",distance);

}   

}

我了解此方法可计算2个点之间的距离.我将需要以某种方式循环遍历我的注释并创建距离.似乎这会更有用,因为它会根据当前的userLocation不断重新计算距离.虽然,我确实对此表示怀疑.一旦知道某物有多远,就很少希望不断提醒它有多远.

I understand this method calculates the distance between 2 points. I would somehow need to cycle thru my annotations and create the distance. It seems this would be the more useful one since it constantly recalculates the distances based on the current userLocation. Although, I do wonder about the effectiveness of that. Once you know how far away something is, you rarely wish to be constantly reminded as to how far away it is.

  1. 部分解决方案B使用以下代码:

`-(void)mapView:(MKMapView *)mapView注解View:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

`- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

CLLocation *pinLocation = [[[CLLocation alloc] initWithLatitude:[(MyAnnotation*)[view annotation] coordinate].latitude longitude:[(MyAnnotation*)[view annotation] coordinate].longitude]];

CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];

CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];

NSLog(@"Distance to pin %4.0f", distance);

} `

在这种情况下,每当敲击引脚时,都会计算距离.但是我不清楚MyAnnotation [查看注释]的代码,我猜测原始海报的位置基于MyAnnotation类,因此我将其更改为MyLocation,除一个错误外,其他所有错误都消失了.由于某种原因,我在最后一个方括号的pinLocation行收到了预期标识符错误.

In this case, whenever the pin is tapped, the distance is calculated. But Im unclear as to the code for MyAnnotation [view annotation], Im guessing the original poster had his locations based off of a MyAnnotation Class so I changed it to MyLocation and all but 1 error went away. I get an Expected Identifier error at the pinLocation line at the last square bracket for some reason.

我觉得解决的办法就在我的舌尖上.只需要一点额外的推动即可:)

I feel the solution is in the tip of my tongue. Just need that little extra push :)

谢谢大家

推荐答案

只要将代码移到calloutAccessoryControlTapped方法内,就在创建MKAnnotation的行之后,无论您在哪里.为MKAnnotation子类提供浮点距离属性,并将其设置为字幕.

Just move the code inside the calloutAccessoryControlTapped method right after the line, wherever you have it, that creates the MKAnnotation. Give MKAnnotation subclass a float distance property and set it as the subtitle.

这篇关于如何增加距离到MKAnotnotation的引脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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