如何在MapView IOS中创建PolyLine

时间:2016-10-17 09:54:37

标签: ios objective-c google-maps

我创建了一个项目。我想通过谷歌方向URL在MapView中绘制PolyLine。我尝试了许多教程和链接,但没有成功绘制PolyLine。请推荐任何教程,如何绘制PolyLine。请帮忙。三江源

3 个答案:

答案 0 :(得分:2)

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

对于带谷歌地图的项目,请看:

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

答案 1 :(得分:1)

如果您使用的是Google地图,我会更喜欢这种方式。

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;
    }
}];}

答案 2 :(得分:0)

请试试这个

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

OR

- (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];

}