【问题标题】:How to get direction from current position to destination using google map IOS6如何使用谷歌地图IOS6获取从当前位置到目的地的方向
【发布时间】:2014-06-06 09:55:22
【问题描述】:

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

我的代码。

 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;

上面的代码我用来指向我的目的地点的制造商,请告诉我如何添加导航方向,我已经卡在这里很长时间了,请帮帮我。

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

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

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

    验证 comgooglemaps-x-callback:// URL 方案是否可用。 启动 iOS 版 Google Maps 应用程序,并请求前往纽约市肯尼迪机场的路线。将起始地址留空以请求来自用户当前位置的路线。 将标有“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.");
     }
    

    【讨论】:

    • 我有目的地纬度和经度,我们可以将其用于当前位置的导航
    • 是的,您可以使用它,或者您可以动态获取该纬度和经度
    • 12.9259,77.6229 这是我的目的地纬度和纬度我想将方向从当前位置路由到这一点如何在代码中使用它
    【解决方案2】:

    回答这个问题..

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

    【讨论】:

    • 把你的目的地坐标放在这里,根据你的需要使用这个方法我希望这能指导你正确的方式
    • 我使用 mkmapview 在印度没有显示任何导航之前我想使用谷歌地图请告诉如何在 ios 中使用谷歌
    • 那么我的朋友,你应该在这里学习 google 的 ios sdk 及其文档:developers.google.com/maps/documentation/ios
    • 我知道兄弟我已经用过钥匙了
    【解决方案3】:

    如果你不想使用 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]]];
          }
       }
    

    【讨论】:

    • 请告诉我是否添加了 mkmapkit 并添加了 oulet,我是否使用过这个请告诉我如何操作
    • 请在这段代码中告诉我必须在你的代码中提供目的地纬度和纬度以及在哪里调用函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2023-03-31
    • 2017-09-27
    • 1970-01-01
    相关资源
    最近更新 更多