如何使用谷歌地图IOS6从当前位置到目的地的方向

时间:2014-06-06 09:55:22

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

您好,在我的应用程序中,我已经集成了谷歌地图,我想提供从我当前位置到目的地点的导航方向,请告诉我如何实现这一点。

我的代码。

 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:12.9259
                                                        longitude:77.6229
                                                             zoom:6];
 mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
 mapView.myLocationEnabled= NO;
 self.view = mapView;
 GMSMarker *marker = [[GMSMarker alloc] init];
 marker.position = CLLocationCoordinate2DMake(12.9259, 77.6229);
 marker.title = @"Hello World";
 marker.snippet = @"bangalore";
 marker.map = mapView;

以上代码我已经习惯指向我的目的地点的制造商,请告诉我如何添加导航方向,我已经被困在这里很长时间请帮帮我。

感谢。

3 个答案:

答案 0 :(得分:1)

您可以使用comgooglemaps://或comgooglemaps-x-callback://网址方案。

注意:通过x-source参数添加的按钮不会显示在精细导航UI中。

以下代码段显示了如何使用comgooglemaps-x-callback://方案来请求路线,然后在用户准备好后返回到您的应用。代码将执行以下操作:

验证comgooglemaps-x-callback:// URL方案是否可用。 启动Google Maps for iOS应用,并请求前往纽约肯尼迪国际机场的路线。将起始地址留空以从用户的当前位置请求路线。 将标有“AirApp”的按钮添加到Google Maps for iOS App。按钮标签由x-source参数定义。 当用户单击后退按钮时,调用虚构的URL方案sourceapp://。 代码如下所示。

NSURL *testURL = [NSURL URLWithString:@"comgooglemaps-x-callback://"];
  if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
   NSString *directionsRequest = @"comgooglemaps-x-callback://" +
    @"?  daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
      @"&x-success=sourceapp://?resume=true&x-source=AirApp";
 NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
 [[UIApplication sharedApplication] openURL:directionsURL];
  }
 else
 {
     NSLog(@"Can't use comgooglemaps-x-callback:// on this device.");
 }

答案 1 :(得分:0)

回答这个答案..

- (IBAction)handleRoutePressed:(id)sender {
    // We're working
    self.activityIndicator.hidden = NO;
    [self.activityIndicator startAnimating];
    self.routeButton.enabled = NO;
    self.routeDetailsButton.enabled = NO;

    // Make a directions request
    MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
    // Start at our current location
    MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
    [directionsRequest setSource:source];
    // Make the destination
    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(38.8977, -77.0365);
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
    [directionsRequest setDestination:destination];

    MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        // We're done
        self.activityIndicator.hidden = YES;
        [self.activityIndicator stopAnimating];
        self.routeButton.enabled = YES;

        // Now handle the result
        if (error) {
            NSLog(@"There was an error getting your directions");
            return;
        }

        // So there wasn't an error - let's plot those routes
        self.routeDetailsButton.enabled = YES;
        self.routeDetailsButton.hidden = NO;
        _currentRoute = [response.routes firstObject];
        [self plotRouteOnMap:_currentRoute];
    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[SCStepsViewController class]]) {
        SCStepsViewController *vc = (SCStepsViewController   *)segue.destinationViewController;
        vc.route = _currentRoute;
    }
}

#pragma mark - Utility Methods
- (void)plotRouteOnMap:(MKRoute *)route
{
    if(_routeOverlay) {
        [self.mapView removeOverlay:_routeOverlay];
    }

    // Update the ivar
    _routeOverlay = route.polyline;

    // Add it to the map
    [self.mapView addOverlay:_routeOverlay];

}

#pragma mark - MKMapViewDelegate methods
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:        (id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
    renderer.strokeColor = [UIColor redColor];
    renderer.lineWidth = 4.0;
    return  renderer;
}

答案 2 :(得分:-1)

如果您不想使用NSURL,那么您只需使用[MKMapItem openMapsWithItems:]

 - (void)openMapsWithDirectionsTo:(CLLocationCoordinate2D)to {
        Class itemClass = [MKMapItem class];
        if (itemClass && [itemClass res  pondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
          MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
         MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil] autorelease]];
        toLocation.name = @"Destination";
        [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
                     launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil]
                                                             forKeys:[NSArray  arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
    [toLocation release];
}
      else {
        NSMutableString *mapURL = [NSMutableString   stringWithString:@"http://maps.google.com/maps?"];
    [mapURL appendFormat:@"saddr=Current Location"];
    [mapURL appendFormat:@"&daddr=%f,%f", to.latitude, to.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[mapURL   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
      }
   }