使用google map api和X-callback url

时间:2014-08-31 22:17:08

标签: ios google-maps

我正在开发一个导航应用,当用户点击Apple地图上显示的任何一个针脚时,该应用会从我的原生应用启动GOOGLE MAP。位置数据来自GOOGLE。因此,我将此代码用于显示标注附件的块,并让用户访问GOOGLE MAP

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
 {

    [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
    NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
    NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:url];
 }

然后我发现有一个x-callback网址,可以在google地图中放置一个后退按钮,让用户返回我的原生应用。但谁知道如何使用这些代码行?我已经在属性列表中注册了我的应用程序,但我不知道下一步该做什么。来自谷歌的代码是这样的:

comgooglemaps-x-callback://?center=40.765819,-73.975866&zoom=14
&x-success=sourceapp://?resume=true
&x-source=SourceApp

任何人都可以告诉我在哪里可以放这些代码?以及如何在我的应用程序中使用它?

3 个答案:

答案 0 :(得分:0)

如果您在Google文档中向下滚动,则会提供有关如何在iOS中使用此代码的示例代码。 提供的代码:

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."); 
}

在爸爸=你的地址后,你会改变一切。同时将AirApp更改为您的应用程序名称。

答案 1 :(得分:0)

你应该使用以下代码:

    NSURL *testURL = [NSURL URLWithString:@"comgooglemaps-x-callback://"];
    if ([[UIApplication sharedApplication] canOpenURL:testURL]) 
    {
       NSMutableString *directionsRequest = [NSMutableString   stringWithString:@"comgooglemaps-x-callback://"];
       [directionsRequest appendFormat:@"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York"];
       [directionsRequest appendFormat:@"&x-success=CouriorMap://?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.");
    }

答案 2 :(得分:0)

使用谷歌地图回拨功能:

  1. 将以下内容添加到info.plist

        <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLName</key>
                <string>com.example.GoogleMapTest</string>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>myapp</string>
                </array>
            </dict>
        </array>
    </dict>
    </plist>
    
  2. com.example.GoogleMapTest是您的应用包ID。

    myapp是与Google地图的comgooglemaps://。

    一样的网址方案
    1. 在任何地方调用该函数:

      - (void)useGoogleMapApp {
          if ([[UIApplication sharedApplication] canOpenURL:
               [NSURL URLWithString:@"comgooglemaps://"]]) {
      
             NSString *url = @"comgooglemaps-x-callback://?saddr=1.294094,103.853722&daddr=1.244310,103.833386&directionsmode=transit&x-success=myapp://?resume=true&x-source=MyAppName";
      
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
          } else {
              NSLog(@"Can't use comgooglemaps://");
          }
      }