当我使用 Google Maps SDK 时,我试图在 iOS 上获取两个位置之间的行车路线。我知道我们可以使用两种方法来做到这一点:-
1.) 使用 URL Scheme,为此您的设备上必须安装 Google Maps App。
2.) 使用 Directions API,通过请求-响应然后解析 JSON。显示标记以指示方向。
现在,我的问题是我可以通过其他方式在 iOS 上执行此操作吗?我需要显示从我当前位置到我有纬度/经度的特定位置的方向。
我的意思是真的不可能简单地将 2 个位置作为参数传递,Google Maps SDK 会给我指示吗?
谢谢,
最佳答案
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
mapView.myLocation.coordinate.latitude,
mapView.myLocation.coordinate.longitude,
destLatitude,
destLongitude,
@"Your Google Api Key String"];
NSURL *directionsURL = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:directionsURL];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"%@",response);
NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:&error];
GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
singleLine.strokeWidth = 7;
singleLine.strokeColor = [UIColor greenColor];
singleLine.map = self.mapView;
}
else NSLog(@"%@",[request error]);
注意:确保您的 Google Direction API Sdk 在您的 Google 开发者控制台中启用。
关于ios - Google Maps iOS SDK,获取两个位置之间的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20905797/