简体   繁体   English

在GMSMapView(Google Maps iOS SDK)上绘制路线

[英]Draw a route on GMSMapView (Google Maps iOS SDK)

I want to draw a route from point A to point B on my mapView ( GMSMapView ). 我想在mapViewGMSMapView )上绘制从A点到B点的路线。

I've tried to do it like this: 我试图这样做:

GMSMutablePath *path=[GMSMutablePath path];
    [path addCoordinate:self.locationManager.location.coordinate];
    [path addCoordinate:marker.position];

    GMSPolyline *rectangle=[GMSPolyline polylineWithPath:path];
    rectangle.strokeWidth=2.f;
    rectangle.map=self.mapView;

But it just drew a line that connects point A to point B but not the route itself (the directions through streets, roads etc...). 但是它只是画了一条线,将A点连接到B点,而不是路线本身(通过街道,道路等的方向)。

I want to draw on the map the best way to get to point B from point A by walking. 我想在地图上绘制从步行到A点到达B点的最佳方法。

How can I do that? 我怎样才能做到这一点?

Thank you! 谢谢!

Use google maps direction API. 使用Google Maps Direction API。 Send starting origin and destination location point to it using NSURLConnection or any other 3rd Party library like AFNetworking etc. Get Response in XML or JSON and parse it. 使用NSURLConnection或任何其他第三方库(如AFNetworking等)向其发送起始起点和终点位置点。以XML或JSON获取响应并进行解析。

{ dispatch_async(dispatch_get_main_queue(), ^{ {dispatch_async(dispatch_get_main_queue(),^ {

        PlayBackObject *displayobj=[[PlayBackObject alloc]init];

        displayobj=[playBackMutArray objectAtIndex:0];

        double distanceLatitude = [displayobj.latitudeStr doubleValue];
        double distanceLongtitude =[displayobj.longitudeStr doubleValue];

        CLLocationDegrees lat = distanceLatitude;
        CLLocationDegrees lon =distanceLongtitude;

        GMSCameraPosition* camera = [GMSCameraPosition
                                     cameraWithLatitude:lat
                                     longitude:lon
                                     zoom: 9];


        GMSMapView *ListAlertMultimap;



            ListAlertMultimap =[GMSMapView mapWithFrame: CGRectMake(0, 0, 414,736) camera: camera];
            [GmapPlayView addSubview:ListAlertMultimap];



          path = [GMSMutablePath path];

        for (int i = 0; i < [playBackMutArray count]-1; i++)

        {

            double lat = [[[playBackMutArray objectAtIndex:i]valueForKey:@"latitudeStr" ] doubleValue];
            double lng = [[[playBackMutArray objectAtIndex:i]valueForKey:@"longitudeStr" ] doubleValue];

            marker = [[GMSMarker alloc] init];

            marker.position = CLLocationCoordinate2DMake(lat,lng);

            marker.appearAnimation = kGMSMarkerAnimationPop;
            NSString *title=[[playBackMutArray objectAtIndex:i]valueForKey:@"trackeridStr"];
            NSString *speed=[[playBackMutArray objectAtIndex:i]valueForKey:@"speedStr"];
            NSString *direction=[[playBackMutArray objectAtIndex:i]valueForKey:@"directionStr"];

            marker.title=[NSString stringWithFormat:@"Tracker ID : %@",title];
            marker.snippet=[NSString stringWithFormat:@"Speed : %@km/hr Direction : %@",speed,direction];

 //         marker.icon = [UIImage imageNamed:@"marker_start"];

            [ListAlertMultimap setMinZoom:2 maxZoom:25];
            marker.map = ListAlertMultimap;

            [path addLatitude:lat longitude:lng];
        }
           NSLog(@"Direction path");
           polyline = [GMSPolyline polylineWithPath:path];
           polyline.strokeColor = [UIColor blueColor];
           polyline.strokeWidth = 5.f;
           polyline.map=ListAlertMultimap;

    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM