如何使用谷歌地图sdk为ios创建路线图

时间:2014-06-07 10:45:11

标签: ios google-maps google-maps-sdk-ios

您好,在我的应用程序中,我必须提供到我的目的地坐标的路线,所以我在我的应用程序中集成了谷歌地图,但我不知道如何为此提供路线导航。经过长时间的搜索我得到了一些示例代码就像地图中的磁带位置一样,它会在地图上显示路线方向。

这是我的代码。

 (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

感谢。

1 个答案:

答案 0 :(得分:0)

你需要做的只是选择两个点,并给它谷歌给你一个路线的JSON,它将在你的地图上绘制它。这就是我所做的绘制从设备位置到目的地点的路线:

-(void) updateRoute {
    self.isMapUpdated = YES;
    CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(
                                                                  self.locationManager.location.coordinate.latitude,
                                                                  self.locationManager.location.coordinate.longitude);
    GMSMarker *marker2 = [GMSMarker markerWithPosition:position2];
    marker2.map = self.mapView;
    marker2.title = @"I AM HERE!!";
    [waypoints_ addObject:marker2];
    NSString *positionString2 = [[NSString alloc] initWithFormat:@"%f,%f",
                                 self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude];
    [waypointStrings_ addObject:positionString2];


    CLLocationCoordinate2D position1 = CLLocationCoordinate2DMake(                                                         destination.LAT,                                                         destination.LONG);
    GMSMarker *marker1 = [GMSMarker markerWithPosition:position1];
    marker1.map = self.mapView;
    marker1.icon = [UIImage imageNamed:@"marker.png"];
    [waypoints_ addObject:marker1];
    NSString *positionString1 = [[NSString alloc] initWithFormat:@"%f,%f",
                                 destination.LAT,destination.LONG];
    [waypointStrings_ addObject:positionString1];

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 {

    if([json objectForKey:@"routes"] != nil)
        if([[json objectForKey:@"routes"] count] != 0){
            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.strokeColor = [Utility colorWithHexString:@"790566"];
            polyline.strokeWidth = 3.0;
            polyline.map = self.mapView;
        }
}

此外,您需要将MDDirectionService标题和主要文件导入项目,然后将标题文件添加到您的课程中,您可以从Google提供的简单应用程序中下载这些文件。