简体   繁体   中英

How to create route map using the google maps sdk for ios

Hi in my application i have to give route to my destination coordinates so i have integrated google map in my application but i dont how to give route navigation for the that. After long search i got some sample code. Its like if tape locations in map it will show the route direction on the map.

This is my smaple code.

 (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_;
}

The above code will show the route location when u tape on the map i want to do like my current location to my destination coordinate please tell me how to achieve this one.

for example this my destination coordinate 12.9259,77.6229

Thanks.

All you need to do is just select two points and give it too google to give you a route's JSON and it will draw it on your map. This is what I did which draw a route from device's location to your destination point :

-(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;
        }
}

Also you need MDDirectionService header and main files to import to your project and add the header file to your class which you can download them from the simple app which Google provided it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM