【发布时间】:2014-06-07 10:45:11
【问题描述】:
嗨,在我的应用程序中,我必须提供到目的地坐标的路线,因此我在应用程序中集成了谷歌地图,但我不知道如何为此提供路线导航。经过长时间的搜索,我得到了一些示例代码。就像如果在地图中使用磁带位置,它会在地图上显示路线方向。
这是我的示例代码。
(void)loadView {
waypoints_ = [[NSMutableArray alloc]init];
waypointStrings_ = [[NSMutableArray alloc]init];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:12.9259
longitude:77.6229
zoom:13];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.delegate = self;
self.view = mapView_;
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:
(CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
coordinate.latitude,
coordinate.longitude);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView_;
[waypoints_ addObject:marker];
NSString *positionString = [[NSString alloc] initWithFormat:@"%f,%f",
coordinate.latitude,coordinate.longitude];
[waypointStrings_ addObject:positionString];
if([waypoints_ count]>1){
NSString *sensor = @"false";
NSArray *parameters = [NSArray arrayWithObjects:sensor, waypointStrings_,
nil];
NSArray *keys = [NSArray arrayWithObjects:@"sensor", @"waypoints", nil];
NSDictionary *query = [NSDictionary dictionaryWithObjects:parameters
forKeys:keys];
MDDirectionService *mds=[[MDDirectionService alloc] init];
SEL selector = @selector(addDirections:);
[mds setDirectionsQuery:query
withSelector:selector
withDelegate:self];
}
}
- (void)addDirections:(NSDictionary *)json {
NSDictionary *routes = [json objectForKey:@"routes"][0];
NSDictionary *route = [routes objectForKey:@"overview_polyline"];
NSString *overview_route = [route objectForKey:@"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = mapView_;
}
上面的代码将显示路线位置,当你在地图上贴上我想做的像我当前的位置到我的目的地坐标时,请告诉我如何实现这一点。
例如这是我的目的地坐标 12.9259,77.6229
谢谢。
【问题讨论】:
-
/你要画路径吗?从当前位置到目的地?
标签: ios google-maps google-maps-sdk-ios