【发布时间】:2016-04-19 05:28:11
【问题描述】:
所以,我在我的应用中使用了路线 API。但是,当我从响应中解码折线时,它没有正确显示。这是我的代码:
//Construct request URL
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
userMarker.position.latitude,
userMarker.position.longitude,
place.coordinate.latitude,
place.coordinate.longitude,
@"AIzaSyDrtHA-AMiVVylUPcp46_Vf1eZJJFBwRCY"];
NSURL *directionsURL = [NSURL URLWithString:urlString];
//Get directions in JSON format
dispatch_async(dispatch_get_main_queue(), ^{
NSData* data = [NSData dataWithContentsOfURL:directionsURL];
NSError* error;
if(data){
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//Parse JSON and plot route on map
NSDictionary *routes = [json objectForKey:@"routes"][0];
NSDictionary *route = [routes objectForKey:@"overview_polyline"];
NSString *overview_route = [route objectForKey:@"points"];
//Clear map from previous polylines
[self.mapView clear];
//Make polyline
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 4;
polyline.strokeColor = [UIColor darkGrayColor];
polyline.map = self.mapView;
});
这里是折线:
您可以看到它没有正确地跟随道路。但是,在我看来,似乎没有足够的点来进行适当的弯曲。
编辑:这只发生在 50% 的时间里,有时会正确显示,有时则不会。
我可能做错了什么?
【问题讨论】:
标签: ios decode google-maps-sdk-ios