我可以创建具有纬度和经度的GMSPath而不是pathFromEncodedPath吗?

dur*_*zno 2 google-maps objective-c gmsmapview

我一直在关注这个教程,它可以帮助我在地图上的2个抽头位置之间绘制路线方向(折线).但是我不需要点击位置,因为当我想向用户显示路线时,我已经拥有了原点和目的地的纬度和经度.

我可以使用我已经拥有的纬度和经度数组创建GMSPath吗?如果是这样,怎么样?

- (void)addDirections:(NSDictionary *)json {

NSDictionary *routes = [json objectForKey:@"routes"][0];

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


GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = self.mainGoogleMap;}
Run Code Online (Sandbox Code Playgroud)

chi*_*hah 6

是的,您可以通过以下方法完成.

首先,您需要创建NSMutablePath全局对象.

然后你必须在该路径中添加纬度和经度,如下所示,然后将该路径指定给折线对象,并绘制折线

在viewDidLoad中

 pathDynamic = [[GMSMutablePath alloc] init];
Run Code Online (Sandbox Code Playgroud)

当您想要在可变路径中添加位置时

   CLLocation *loc = [[CLLocation alloc] initWithLatitude:[[dictData valueForKey:@"fl_route_latitude"] doubleValue] longitude:[[dictData valueForKey:@"fl_route_longitude"] doubleValue]];

    [pathDynamic addCoordinate:loc.coordinate];//Add your location in mutable path

    GMSPolyline *polyline = [GMSPolyline polylineWithPath:pathDynamic];
    // Add the polyline to the map.
    polyline.strokeColor = AppOrangeColor;
    polyline.strokeWidth = 3.0f;
    polyline.map = [self getMapView];
Run Code Online (Sandbox Code Playgroud)