google map static drawing “route” instead of straight line

后端 未结 4 1680
傲寒
傲寒 2020-12-15 11:48

I succeeded in getting a google static map displaying a path between 2 coordinates.

The problem is that the drawn path is just a straight line between the 2 points.<

相关标签:
4条回答
  • 2020-12-15 11:55

    Using Polylines u can draw straight line.

    The Polyline class defines a linear overlay of connected line segments on the map. A Polyline object consists of an array of LatLng locations, and creates a series of line segments that connect those locations in an ordered sequence.

    u can see the example here

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

    about polylines

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

    0 讨论(0)
  • 2020-12-15 12:11

    I think you cannot use this functionnality with the staticmap api. However you can use Directions with the JavaScript API V3.

    0 讨论(0)
  • 2020-12-15 12:17

    You can definitely do this with the Static Maps API:

    get directions using DirectionsService:

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

    and convert the overview path to suit the requirements of the Static Maps API:

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

    0 讨论(0)
  • 2020-12-15 12:22

    I dugged into many suggestions and codes and combined all to make a very simple solution, the code above should work for you:

    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_;
    }
    
    0 讨论(0)
提交回复
热议问题