Iphone 使用mapkit确定距离
如何使用mapkit确定1000英尺或1/2英里的距离?某个销的半径或两个销之间的距离Iphone 使用mapkit确定距离,iphone,cocoa-touch,mapkit,Iphone,Cocoa Touch,Mapkit,如何使用mapkit确定1000英尺或1/2英里的距离?某个销的半径或两个销之间的距离 例如,我将地图居中放置在插脚A上。插脚B、C和D也位于地图上与插脚A的不同距离处。B和C与A的距离在1/2英里以内,但D距离1英里。我想知道B和C与A的距离在1/2英里以内。我如何计算呢?既然您已经说过两个不同的点是“管脚”,我假设您使用的是MKPinAnnotationView(或其他一些注释视图)。如果没有,你将不得不以其他方式找到位置 如果有指向这些位置的注释对象的指针,则可以轻松地对其调用-coord
例如,我将地图居中放置在插脚A上。插脚B、C和D也位于地图上与插脚A的不同距离处。B和C与A的距离在1/2英里以内,但D距离1英里。我想知道B和C与A的距离在1/2英里以内。我如何计算呢?既然您已经说过两个不同的点是“管脚”,我假设您使用的是MKPinAnnotationView(或其他一些注释视图)。如果没有,你将不得不以其他方式找到位置 如果有指向这些位置的注释对象的指针,则可以轻松地对其调用
-coordinate
,从这些坐标创建CLLocations(使用-initWithLatitude:longitude:
),然后使用方法-getDistanceFrom
检索以米为单位的距离。从那里,很容易转换成英里数。总而言之,代码应该是这样的:
CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];
double distanceMeters = [pointALocation getDistanceFrom:pointBLocation];
double distanceMiles = (distanceMeters / 1609.344);
你将得到以英里为单位的距离,并可以从那里进行比较。希望这能有所帮助。getDistanceFrom现在已被弃用,因此这里有一个备选答案供任何希望这样做的人使用
CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];
float distanceMiles = (distanceMeters / 1609.344);
[pointALocation release];
[pointBLocation release];
如上所述,您可以使用float
而不是double
,如果不需要float
的精度,您可以将结果转换为int,如下所示:
int distanceCastToInt = (int) [pointALocation distanceFromLocation:pointBLocation];
pointAAnnotation.title = @"Point A";
pointAAnnotation.subtitle = [NSString stringWithFormat: @"Distance: %im",distanceCastToInt];
如果您想在注释中大致了解距离,则int
非常方便,如下所示:
int distanceCastToInt = (int) [pointALocation distanceFromLocation:pointBLocation];
pointAAnnotation.title = @"Point A";
pointAAnnotation.subtitle = [NSString stringWithFormat: @"Distance: %im",distanceCastToInt];
注释的副标题应为“距离:50m”,例如。这与给定答案相当:
let pointACoordinate = pointAAnnotation.coordinate
let pointALocation = CLLocation(latitude: pointACoordinate.latitude, longitude: pointACoordinate.longitude)
let pointBCoordinate = pointBAnnotation.coordinate
let pointBLocation = CLLocation(latitude: pointBCoordinate.latitude, longitude: pointBCoordinate.longitude)
let distanceMeters = pointALocation.distance(from: pointBLocation)
let distanceMiles = (distanceMeters / 1609.344)
你能解释一下你的问题吗,这样我们就可以了解确切的要求了?我已经编辑了这个问题。如果你需要更多的信息,请告诉我。好的,但这样我们可以得到两点之间的直线距离。。。不是实际的一个如何找到这个实际的距离。。。。