【发布时间】:2016-10-17 09:54:36
【问题描述】:
我已经创建了一个项目。我想通过谷歌方向 URL 在 MapView 中绘制一条折线。我尝试了许多教程和链接,但没有成功绘制折线。请建议任何教程,如何绘制折线。请帮忙。谢谢你
【问题讨论】:
标签: ios objective-c google-maps
我已经创建了一个项目。我想通过谷歌方向 URL 在 MapView 中绘制一条折线。我尝试了许多教程和链接,但没有成功绘制折线。请建议任何教程,如何绘制折线。请帮忙。谢谢你
【问题讨论】:
标签: ios objective-c google-maps
GMSPolyline *poly = [GMSPolyline polylineWithPath:path];
poly.strokeColor = [UIColor purpleColor];
poly.tappable = TRUE;
poly.map = self.googleMapView;
对于带有谷歌地图的项目,请参见:
【讨论】:
如果您使用的是 GOOGLE 地图,我更喜欢这种直截了当的方式。
GMSPolyline *polyPath = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
以下是完整的代码sn-p。
-(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;
}
}];}
【讨论】:
[[NSURLSession sharedSession] dataTaskWithRequest:...]替换已弃用的[NSURLConnection sendAsynchronousRequest:...]
请试试这个: 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];
}
【讨论】: