使用mapkit确定距离 [英] Determining distance using mapkit

查看:148
本文介绍了使用mapkit确定距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用mapkit确定1000英尺或1/2英里的距离?

How is a 1000ft or 1/2 mile distance determined with mapkit? Either a radius from some pin or the distance between two pins.

例如,我将地图定位在引脚A上。引脚B,C和D也位于引脚A上。地图距离别针A不同距离.B和C在距离A的1/2英里内,但D距离1英里。我想知道B和C距离A的1/2英里内如何计算?

For example, I center the map on pin A. Pins B, C, and D are also on the map at various distances from pin A. B and C are within 1/2 mile from A but D is 1 mile away. I'd like to know that B and C are within 1/2 mile from A. How can I calculate that?

推荐答案

因为你说两个不同的点是pins,我想假设你使用MKPinAnnotationView(或一些其他注释视图)。

Since you've stated that the two different points are "pins", I'm going to assume you're using MKPinAnnotationView (or some other annotation view). If not, you're going to have to get the location some other way.

如果你有这些位置的注释对象的指针,那么你可以很容易地调用 -coordinate ,从这些坐标创建CLLocations(使用 -initWithLatitude:longitude:),然后使用方法 -getDistanceFrom 以米为单位检索距离。从那里,它是一个容易转换成英里。所有的代码看起来像这样:

If you have pointers to the annotation objects for these locations, then you can easily call -coordinate on them, create CLLocations from these coordinates (using -initWithLatitude:longitude:), and then use the method -getDistanceFrom to retrieve the distance in meters. From there, it's an easy conversion to miles. All told, the code would look something like this:

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

你将以距离为单位,以英里为单位,并从那里进行比较。希望这有助于。

You'll end up with the distance in miles, and can compare it from there. Hope this helps.

这篇关于使用mapkit确定距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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