我已经创建了一个基于地图的应用程序。现在通过google方向API获取PolyLine点。但是如何用这个GSMPath
来画多边线。我尝试了很多次,但没有在GMSMapView
中绘制polyLine。如何画请提出建议。 使用哪种方法请引导。如何在MapView中绘制GMSPolyline
-(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;
不兼容的指针类型分配给 'GMSMapView中* _Nullable' 从 '的MKMapView *'
请建议如何绘制折线。 Thankyou
感谢您的回复但AFHTTPNetwork设置不使用。因为我试过但没有成功在Xcode中设置。 –
无需AFHTTPNetwork设置。只需调用您的服务调用方法来调用上述服务并获取响应数组。 我已经在我的现场项目中使用这段代码,它工作的很好。不要忘记评论。 –
你 - (void)setMapDataValue哪一次调用InViewDidLoad? –