如何在iOS中以文本形式获取两个位置之间的路线
Posted
技术标签:
【中文标题】如何在iOS中以文本形式获取两个位置之间的路线【英文标题】:How to get directions between two locations in the form of text in iOS 【发布时间】:2015-03-31 20:41:33 【问题描述】:我记得有一个 Google API 需要 lat、long 或 from&to 地址,如果我们在 webview 上加载该 url,它会以文本的形式显示两点之间的方向。任何人都可以为我提供获取基于文本的方向的 API。我不想在地图上画线,想显示文字。
【问题讨论】:
【参考方案1】:MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.776142, -122.424774) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
[srcMapItem setName:@""];
MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.73787, -122.373962) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
[distMapItem setName:@""];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
[request setSource:srcMapItem];
[request setDestination:distMapItem];
[request setTransportType:MKDirectionsTransportTypeAny];
MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error)
NSLog(@"response = %@",response);
NSArray *arrRoutes = [response routes];
[arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
MKRoute *rout = obj;
MKPolyline *line = [rout polyline];
[mapview addOverlay:line];
NSLog(@"Rout Name : %@",rout.name);
NSLog(@"Total Distance (in Meters) :%f",rout.distance);
NSArray *steps = [rout steps];
NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);
NSMutableArray *stepsArray=[[NSMutableArray alloc] init];
[steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
NSLog(@"Rout Instruction : %@",[obj instructions]);
NSLog(@"Rout Distance : %f",[obj distance]);
[stepsArray addObject:[obj instructions]];
];
[self myShowDirections:response];
self.steps.text=[NSString stringWithFormat:@"%@",stepsArray];
你可以使用它并得到你想要的。
【讨论】:
以上是关于如何在iOS中以文本形式获取两个位置之间的路线的主要内容,如果未能解决你的问题,请参考以下文章
如何在从 ListView 项获取的两个地理点之间绘制标记和路线?
如何在苹果地图中的两个位置之间绘制路线。通过 openURL