【问题标题】:Google Maps decoded GMSPath incorrect iOS谷歌地图解码 GMSPath 不正确 iOS
【发布时间】:2016-04-19 05:28:11
【问题描述】:

所以,我在我的应用中使用了路线 API。但是,当我从响应中解码折线时,它没有正确显示。这是我的代码:

    //Construct request URL
    NSString *urlString = [NSString stringWithFormat:
                           @"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
                           @"https://maps.googleapis.com/maps/api/directions/json",
                           userMarker.position.latitude,
                           userMarker.position.longitude,
                           place.coordinate.latitude,
                           place.coordinate.longitude,
                           @"AIzaSyDrtHA-AMiVVylUPcp46_Vf1eZJJFBwRCY"];

    NSURL *directionsURL = [NSURL URLWithString:urlString];


    //Get directions in JSON format
    dispatch_async(dispatch_get_main_queue(), ^{
        NSData* data = [NSData dataWithContentsOfURL:directionsURL];

        NSError* error;

        if(data){
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

            //Parse JSON and plot route on map
            NSDictionary *routes = [json objectForKey:@"routes"][0];

            NSDictionary *route = [routes objectForKey:@"overview_polyline"];
            NSString *overview_route = [route objectForKey:@"points"];

            //Clear map from previous polylines
            [self.mapView clear];

            //Make polyline
            GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
            GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
            polyline.strokeWidth = 4;
            polyline.strokeColor = [UIColor darkGrayColor];
            polyline.map = self.mapView;
});

这里是折线:

您可以看到它没有正确地跟随道路。但是,在我看来,似乎没有足够的点来进行适当的弯曲。

编辑:这只发生在 50% 的时间里,有时会正确显示,有时则不会。

我可能做错了什么?

【问题讨论】:

    标签: ios decode google-maps-sdk-ios


    【解决方案1】:

    按此进行(使用 js API 而不是您正在使用的 json API):

    Get a polyline from Google maps directions V3

    看起来您应该在响应中使用legs 的每个步骤中的行,而不是overview_polyline,这可能只是一个粗略的近似行。

    【讨论】:

    • 谢谢,但为什么它在 50% 的时间里都能完美运行?
    • 我猜测如果完整路径太复杂(例如,超过一定数量的点),overview_polyline 只会设置为路径的简化版本。
    • 我猜你是对的!谢谢!正如谷歌文档所说:我猜你是对的!谢谢!正如 Google Docs 所述:“overview_polyline 包含一个单点对象,该对象包含路线的编码折线表示。这条折线是生成方向的近似(平滑)路径。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    相关资源
    最近更新 更多