我正在使用谷歌地图 iOS sdk来获取方向。通过此链接帮助我在两点之间绘制路线。现在我无法向最终用户提供路线指示(例如:左转、右转)。如何解决这个问题?请帮助我。现在我正在使用以下代码
//Request the url
-(void)getWayPoints:(CLLocationCoordinate2D )origin destinationIS:(CLLocationCoordinate2D)desti{
NSString *oLat = [NSString stringWithFormat:@"%f",origin.latitude];
NSString *oLong = [NSString stringWithFormat:@"%f",origin.longitude];
NSString *dLat = [NSString stringWithFormat:@"%f",desti.latitude];
NSString *dLong = [NSString stringWithFormat:@"%f",desti.longitude];
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?&origin=%@,%@&destination=%@,%@&sensor=false",oLat,oLong,dLat,dLong];
NSLog(@"url : %@", url);
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
origin.latitude,
origin.longitude);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView_;
CLLocationCoordinate2D position1 = CLLocationCoordinate2DMake(
desti.latitude,
desti.longitude);
GMSMarker *marker1 = [GMSMarker markerWithPosition:position1];
marker1.map = mapView_;
NSURL *googleRequestURL=[NSURL URLWithString:url];
NSLog(@"googleRequestURL : %@", googleRequestURL);
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
//Response from URL
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
// NSLog(@"responseData Data: %@", responseData);
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* places = [json objectForKey:@"routes"];
NSDictionary *routes = [json objectForKey:@"routes"][0];
NSDictionary *route = [routes objectForKey:@"overview_polyline"];
NSArray *routes1 = [json objectForKey:@"routes"];
NSArray *legs = [routes1[0] objectForKey:@"legs"];
NSLog(@"legs %@", legs);
NSArray *steps = [legs[0] objectForKey:@"steps"];
NSString *overview_route = [route objectForKey:@"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];
polyline.strokeWidth = 5.f;
polyline.map = mapView_;
}