【发布时间】:2017-01-23 09:12:45
【问题描述】:
我创建了一个基于地图的应用程序。现在通过谷歌方向 api 获取折线点。但是如何用这个GSMPath 绘制折线。我尝试了很多次,但没有在GMSMapView 中绘制折线。怎么画请指教。
请指导使用哪些方法。
-(void) viewWillAppear:(BOOL)animated
{
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
22.6987,
75.8817,
22.6990,
75.8671,
@"AIzaSyCk6NNWO7DxVUEyEW1B6m-YNpdqv5HbyEk"];
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *encodedUrlAsString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithURL:[NSURL URLWithString:encodedUrlAsString]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"RESPONSE: %@",response);
NSString *resSrt = [[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"DATA: %@",resSrt);
if (!error) {
// Success
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSError *jsonError;
json =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
dispatch_sync(dispatch_get_main_queue(), ^{
NSArray *routesArray = [json objectForKey:@"routes"];
NSLog(@"succes===== %@",routesArray);
GMSPolyline *polyline = nil;
if ([routesArray count] > 0)
{
NSDictionary *routeDict = [routesArray objectAtIndex:0];
NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
points = [routeOverviewPolyline objectForKey:@"points"];
NSLog(@"points ==== %@",points);
GMSPath *path = [GMSPath pathFromEncodedPath:points];
polyline = [GMSPolyline polylineWithPath:path];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 5;
polyline.strokeColor=[UIColor redColor];
polyline.map = self.map_view;
}
else
{
}
});
}
else {
//Web server is returning an error
}
}
else {
// Fail
NSLog(@"error : %@", error.description);
}
}] resume];
}
错误显示后使用的代码
在这段代码和这一行polyline.map = self.map_view;
被评论并运行然后没有错误。
此行没有注释并运行然后显示错误。 -[MKMapView updateOverlay:]
导航器问题:在这一行polyline.map = self.map_view;
从 'MKMapView *' 分配给 'GMSMapView * _Nullable' 的指针类型不兼容
请建议如何绘制折线。谢谢你
【问题讨论】:
标签: ios objective-c google-maps mkmapview gmsmapview