计算iPhone中两点之间的距离 [英] Calculate the distance between two points in iphone
问题描述
我正在创建一个需要用户输入两个位置(邮政编码)的应用程序.我的应用程序将计算这两个点之间的行驶距离,并输出结果.用户可以选择添加航点. 我想我将不得不使用Google Maps API并获得带有结果的xml文件,然后解析该xml文件.谁能帮我,因为我不确定该怎么做. 感谢所有帮助.
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.
示例... 开始:BR1 1LR
Example... Start: BR1 1LR
航点:BR2 0LH
Waypoint: BR2 0LH
终点:BR3 4AY
Waypoint: BR3 4AY
目的地:BR1 1LR
Destination: BR1 1LR
推荐答案
我知道这确实很晚,但是我发布了针对此问题的解决方案,以防万一有人需要它.
I know this is really late, but I'm posting my solution to this in case anybody needs it.
首先,我声明了网址(调用google maps api的网址)
Firstly, i declared the URL (the one that calls google maps api)
#define DST_API @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=%@&destinations=%@&units=%@&sensor=false"
接下来,我创建了一个包含以下URL的字符串:
Next I created a string containing this URL:
NSString *URL = [NSString stringWithFormat:DST_API, source, destination, units];
其中源,目标是包含起点和终点的字符串.单位可以是@英制"或@公制". 现在有了URL,我将获得一个xml字符串.为了解析这一点,我使用了TBXML.关于使用哪种XML解析器一直存在着广泛的争论.我很容易使用 TBXML ,它是
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 TBXML as it was easy, and is the fastest.
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;
}
检查根状态是否正常,然后获取XPath表达式的结果://行/元素/距离/值
Once checking the root status is OK, then obtain the result for the XPath expression: //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
如果有人想拥有一个包含路标的URL,那么这里就是
If anyone wants to have a URL to include way points, then here it is
#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.
这篇关于计算iPhone中两点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!