简体   繁体   English

计算iphone中两点之间的距离

[英]Calculate the distance between two points in iphone

I am creating an application that requires the user to input two places (Postal codes). 我正在创建一个应用程序,要求用户输入两个地方(邮政编码)。 My application will calculate the driving-distance between these two points ad output the result. 我的应用程序将计算这两点之间的行驶距离和输出结果。 The user has the option of adding Way-Points. 用户可以选择添加Way-Points。 I think I would have to use the google maps API and get an xml file with the result and then parse the xml file. 我想我必须使用谷歌地图API并获得带有结果的xml文件,然后解析xml文件。 Can anyone help me because i not sure how to do this. 任何人都可以帮助我,因为我不知道该怎么做。 Appreciate all help. 感谢所有帮助。

Example... Start: BR1 1LR 示例...开始:BR1 1LR

Waypoint: BR2 0LH 航点:BR2 0LH

Waypoint: BR3 4AY Waypoint:BR3 4AY

Destination: BR1 1LR 目的地:BR1 1LR

Yes you can calculate the distance ,for that you need to have the latitude and longitude to get the distance.Objective C provides the method to calculate the distance between points here is the example...... 是的你可以计算距离,因为你需要有经纬度才能得到距离。目标C提供了计算点之间距离的方法,这里就是例子......

float distance =[mUserCurrentLocation distanceFromLocation:location1]/1000;

This will provide you the distance in Kilometers. 这将为您提供以公里为单位的距离。

I know this is really late, but I'm posting my solution to this in case anybody needs it. 我知道这已经很晚了,但是如果有人需要,我会发布我的解决方案。

Firstly, i declared the URL (the one that calls google maps api) 首先,我声明了URL(调用google maps api的URL)

#define DST_API @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=%@&destinations=%@&units=%@&sensor=false"

Next I created a string containing this URL: 接下来我创建了一个包含此URL的字符串:

NSString *URL = [NSString stringWithFormat:DST_API, source, destination, units];

Where source, destination are strings containing the start point and end point. 其中source,destination是包含起点和终点的字符串。 units can be @"imperial" or @"metric". 单位可以是@“imperial”或@“metric”。 Now that i had the URL, i would get back an xml string. 现在我有了URL,我会找回一个xml字符串。 To parse this, i used TBXML. 为了解析这个问题,我使用了TBXML。 There is always a large debate about which XML Parser to use. 关于使用哪种XML Parser总是存在很大争议。 I used TBXML as it was easy, and is the fastest . 我使用TBXML很简单,而且速度最快

TBXML *directionsParser = [[TBXML alloc] initWithURL:[NSURL URLWithString:URL]];

// Check if the Overall status is OK
TBXMLElement *root = directionsParser.rootXMLElement;
TBXMLElement *element = [TBXML childElementNamed:@"status" parentElement:root]; 
NSString *value = [TBXML textForElement:element];
if ([value caseInsensitiveCompare:@"OK"] != NSOrderedSame) {
    [directionsParser release];
    return result;
}

Once checking the root status is OK, then obtain the result for the XPath expression: //row/element/distance/value 检查根状态是否正常后,获取XPath表达式的结果:// row / element / distance / value

element = [TBXML childElementNamed:@"row" parentElement:root];
element = [TBXML childElementNamed:@"element" parentElement:element];
element = [TBXML childElementNamed:@"status" parentElement:element];
value = [TBXML textForElement:element];

// Check if the Element status is OK
if ([value caseInsensitiveCompare:@"OK"] != NSOrderedSame) {
        [directionsParser release];
    return result;
}

element = element->parentElement;

element = [TBXML childElementNamed:@"distance" parentElement:element];

//Note if you want the result as text, replace 'value' with 'text'
element = [TBXML childElementNamed:@"value" parentElement:element];

NSString *result = [TBXML textForElement:element];

[directionsParser release];
//Do what ever you want with result

If anyone wants to have a URL to include way points, then here it is 如果有人想要一个包含路径点的URL,那么就在这里

#define DIR_API @"http://maps.googleapis.com/maps/api/directions/xml?origin=%@&destination=%@&waypoints=%@&units=%@&sensor=false"

But to use way points, things work a bit differently as Jano pointed out. 但是,使用方式点,Jano指出,事情的工作方式有点不同。

If you want the road distance you need to ask Google and then parse the resulting XML , using a XML parser and the XPath expression //leg/step/distance/value , which will give you all the distance, that you will have to sum. 如果你想要道路距离,你需要问Google然后解析生成的XML ,使用XML解析器和XPath表达式//leg/step/distance/value ,这将给你所有的距离,你将需要总和。 See this about parsing xml and using XPath expressions. 请参见关于解析XML和使用XPath表达式。

The distanceFromLocation: method will give you the absolute distance between any two CLLocation points, not a driving distance. distanceFromLocation:方法将为您提供任意两个CLLocation点之间的绝对距离,而不是行驶距离。 For that, you must use the Google Maps API as you suspected. 为此,您必须使用您怀疑的Google Maps API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM