ios - Google Maps iOS SDK,获取两个位置之间的路线

标签 ios xcode google-maps cocoa-touch google-maps-sdk-ios

当我使用 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/

相关文章:

ios - CustomCell (TableView) 中的 Swift Button 将参数传递给 targetMethod

IOS:检查 NSString 的一部分

javascript - 谷歌地图请求返回 "Cannot set property ' position' of undefined"

javascript - 放大和缩小谷歌地图时出现不需要的折线渲染

UILabel 上的 iOS 渐变效果

ios - 基于二进制数据设置 UIImageView 的图像

ios - 如何在 Objective-C 中检查一个字符串是否包含另一个字符串?

swift - 连接到 xib 文件的 UIViewController 中无法识别 subview

xcode - 我需要一个包含iPad视网膜显示屏的iOS模拟器版本

android - 返回 ViewPager,我的 GoogleMap fragment 不可见