【问题标题】:How to create route map using the google maps sdk for ios如何使用 google maps sdk for ios 创建路线图
【发布时间】: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


【解决方案1】:

您需要做的只是选择两个点,然后谷歌也给它一个路线的 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 提供的简单应用程序中下载它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多