xcode 计算iphone中两点之间的距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5761977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate the distance between two points in iphone
提问by SEG
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. 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. Can anyone help me because i not sure how to do this. Appreciate all help.
我正在创建一个需要用户输入两个地方(邮政编码)的应用程序。我的应用程序将计算这两个点之间的行驶距离并输出结果。用户可以选择添加航点。我想我将不得不使用谷歌地图 API 并获取带有结果的 xml 文件,然后解析该 xml 文件。任何人都可以帮助我,因为我不知道如何做到这一点。感谢所有帮助。
Example... Start: BR1 1LR
示例...开始:BR1 1LR
Waypoint: BR2 0LH
航点:BR2 0LH
Waypoint: BR3 4AY
航点:BR3 4AY
Destination: BR1 1LR
目的地:BR1 1LR
采纳答案by SEG
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 的那个)
#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. units can be @"imperial" or @"metric". Now that i had the URL, i would get back an xml string. To parse this, i used TBXML. There is always a large debate about which XML Parser to use. I used TBXMLas it was easy, and is the fastest.
其中 source、destination 是包含起点和终点的字符串。单位可以是@"英制"或@"公制"。现在我有了 URL,我会得到一个 xml 字符串。为了解析这个,我使用了 TBXML。关于使用哪种 XML 解析器一直存在很大的争论。我使用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
一旦检查根状态为 OK,则获取 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 指出的那样,事情的运作方式有点不同。
回答by Sabby
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......
是的,你可以计算距离,因为你需要有纬度和经度才能得到距离。Objective C 提供了计算点之间距离的方法,这里是例子......
float distance =[mUserCurrentLocation distanceFromLocation:location1]/1000;
This will provide you the distance in Kilometers.
这将为您提供以公里为单位的距离。
回答by 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. See thisabout parsing xml and using XPath expressions.
如果您想要道路距离,您需要询问 Google,然后使用 XML 解析器和 XPath 表达式解析生成的 XML//leg/step/distance/value
,这将为您提供所有距离,您将不得不求和。请参见本关于解析XML和使用XPath表达式。
回答by Joe Balsamo
The distanceFromLocation: method will give you the absolute distance between any two CLLocation points, not a driving distance. For that, you must use the Google Maps API as you suspected.
distanceFromLocation: 方法将为您提供任意两个 CLLocation 点之间的绝对距离,而不是行驶距离。为此,您必须像您怀疑的那样使用 Google Maps API。