如何在MapView IOS中创建PolyLine



我创建了一个项目。我想在MapView中通过google direction URL画一条PolyLine。我尝试了许多教程和链接,但没有得到任何成功绘制折线。请建议任何教程,如何绘制折线。请帮助。谢谢

GMSPolyline *poly = [GMSPolyline polylineWithPath:path];
poly.strokeColor = [UIColor purpleColor];
poly.tappable = TRUE;
poly.map = self.googleMapView;

和项目与谷歌地图看到这个:

https://developers.google.com/maps/documentation/ios-sdk/

如果你正在使用谷歌地图,我更喜欢这种直接的方式。

GMSPolyline *polyPath = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];

以下是完整的代码片段:

-(void)drawPathFrom:(CLLocation*)source toDestination:(CLLocation*)destination{
NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true", source.coordinate.latitude,  source.coordinate.longitude, destination.coordinate.latitude,  destination.coordinate.longitude];
NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Url: %@", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if(!connectionError){
        NSDictionary *result        = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSArray *routes             = [result objectForKey:@"routes"];
        NSDictionary *firstRoute    = [routes objectAtIndex:0];
        NSString *encodedPath       = [firstRoute[@"overview_polyline"] objectForKey:@"points"];
        GMSPolyline *polyPath       = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
        polyPath.strokeColor        = [UIColor redColor];
        polyPath.strokeWidth        = 3.5f;
        polyPath.map                = _mapView;
    }
}];}

请试试:http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/

- (void) drawRoute:(NSArray *) path {
    NSInteger numberOfSteps = path.count;
    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [path objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;
        coordinates[index] = coordinate;
    }
    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [map addOverlay:polyLine];
}

最新更新