代码之家  ›  专栏  ›  技术社区  ›  Pierre

谷歌地图静态绘制“路线”而不是直线

  •  8
  • Pierre  · 技术社区  · 14 年前

    我成功地得到了一个谷歌静态地图,显示了两个坐标之间的路径。

    问题是绘制的路径只是两点之间的直线。

    我读到,为了能够在静态的谷歌地图上绘制两个点之间的“路线”,比如,沿着道路和城市地理,而不是直线,我需要添加路径的所有坐标/十字路口。

    有人知道解决这个问题的简单方法吗?

    4 回复  |  直到 8 年前
        1
  •  23
  •   Chris Broadfoot    14 年前

    您当然可以使用静态映射API执行此操作:

    使用DirectionService获取方向:

    http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsService

    并转换概述路径以适应静态映射API的要求:

    http://code.google.com/apis/maps/documentation/staticmaps/#Paths

        2
  •  1
  •   vinod_vh    11 年前

    使用多段线可以绘制直线。

    折线类定义地图上连接的线段的线性覆盖。折线对象由一组latlng位置组成,并创建一系列按顺序连接这些位置的线段。

    你可以在这里看到这个例子

    https://google-developers.appspot.com/maps/documentation/javascript/examples/polyline-simple

    关于折线

    https://developers.google.com/maps/documentation/javascript/overlays

        3
  •  0
  •   MatTheCat    14 年前

    我认为您不能在静态映射API中使用此函数。但是你可以用 Directions 使用JavaScriptAPI v3。

        4
  •  -1
  •   Dhia Chase Isley    8 年前

    我研究了许多建议和代码,并将它们结合在一起,得出了一个非常简单的解决方案,上面的代码应该适用于您:

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=driving",[startLat floatValue] ,[startLong floatValue], [endLat floatValue], [endLong floatValue]]];
    
    NSDictionary *PathInfo;//the json of the result
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData =  [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (!error) {
        PathInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];
        GMSPath *path =[GMSPath PathInfo[@"routes"][0][@"overview_polyline"][@"points"]];
        GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
        singleLine.strokeWidth = 3;
        singleLine.strokeColor = [UIColor redColor];
        singleLine.map = mapView_;
    }