自分のネイティブ アプリケーション内から Google マップ iPhone アプリケーションを起動するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/30058

  •  09-06-2019
  •  | 
  •  

質問

Apple 開発者向けドキュメント (リンクは現在無効です) iPhone で Mobile Safari を使用しているときに Web ページにリンクを配置し、それをクリックすると、iPhone に標準で提供されている Google マップ アプリケーションが起動すると説明しています。

自分のネイティブ iPhone アプリケーション内から特定の住所で同じ Google マップ アプリケーションを起動するにはどうすればよいですか (例:Mobile Safari を介した Web ページではありません)、連絡先で住所をタップすると地図が起動するのと同じように?

注記:これはデバイス自体でのみ機能します。シミュレーターにはありません。

役に立ちましたか?

解決

iOS 5.1.1 以前の場合は、 openURL の方法 UIApplication. 。通常の iPhone の魔法の URL 再解釈が実行されます。それで

[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]

Google マップ アプリを呼び出す必要があります。

iOS 6 からは、Apple 独自のマップ アプリを呼び出すことになります。このために、 MKMapItem 表示したい場所をオブジェクトに指定して送信します。 openInMapsWithLaunchOptions メッセージ。現在の場所から開始するには、次のことを試してください。

[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];

このためには、MapKit に対してリンクする必要があります (そして、位置情報へのアクセスを求めるプロンプトが表示されると思います)。

他のヒント

その通り。これを実現するために必要なコードは次のようなものです。

UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];

以来 ドキュメントに従って, UIApplication は、sharedApplication を呼び出さない限り、アプリケーション デリゲートでのみ使用できます。

特定の座標で Google マップを開くには、次のコードを試してください。

NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

latlong 文字列を CoreLocation の現在の場所に置き換えることができます。

(「z」) フラグを使用してズーム レベルを指定することもできます。値は 1 ~ 19 です。以下に例を示します。

[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

App Store の Google マップ アプリもリリースされました。こちらのドキュメントに記載されています。 https://developers.google.com/maps/documentation/ios/urlscheme

したがって、最初にそれがインストールされていることを確認します。

[[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];

そして、条件付きで置き換えることができます http://maps.google.com/maps?q=comgooglemaps://?q=.

マップ リンクに関する Apple URL スキーム リファレンスは次のとおりです。 https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

有効なマップ リンクを作成するためのルールは次のとおりです。

  • ドメインは google.com 、サブドメインは Maps または ditu である必要があります。
  • クエリにキーとして site が含まれ、値として local が含まれる場合、パスは /、/maps、/local、または /m である必要があります。
  • パスを /maps/* にすることはできません。
  • すべてのパラメータがサポートされている必要があります。サポートされているパラメータのリストについては、表 1 を参照してください**。
  • 値が URL の場合、パラメーターを q=* にすることはできません (そのため、KML は選択されません)。
  • パラメーターに view=text または dirflg=r を含めることはできません。

**サポートされているパラメータのリストについては、上のリンクを参照してください。

iOS 10 を使用している場合は、Info.plist にクエリ スキームを追加することを忘れないでください。

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>comgooglemaps</string>
</array>

object-c を使用している場合

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
    NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else { 
    NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    }

Swift2.2を使用している場合

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: string)!)
}

Swift3.0を使用している場合

if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: string)!)
}

電話での質問ですが、シミュレータでテストしていますか?これはデバイス自体でのみ機能します。

また、openURL は bool を返します。これを使用して、実行しているデバイスがその機能をサポートしているかどうかを確認できます。たとえば、iPod Touch では電話をかけることはできません :-)

このメソッドを呼び出して、これと同じように Google マップ URL スキームを .plist ファイルに追加するだけです 答え.

スウィフト-4 :-

func openMapApp(latitude:String, longitude:String, address:String) {

    var myAddress:String = address

    //For Apple Maps
    let testURL2 = URL.init(string: "http://maps.apple.com/")

    //For Google Maps
    let testURL = URL.init(string: "comgooglemaps-x-callback://")

    //For Google Maps
    if UIApplication.shared.canOpenURL(testURL!) {
        var direction:String = ""
        myAddress = myAddress.replacingOccurrences(of: " ", with: "+")

        direction = String(format: "comgooglemaps-x-callback://?daddr=%@,%@&x-success=sourceapp://?resume=true&x-source=AirApp", latitude, longitude)

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
    //For Apple Maps
    else if UIApplication.shared.canOpenURL(testURL2!) {
        var direction:String = ""
        myAddress = myAddress.replacingOccurrences(of: " ", with: "+")

        var CurrentLocationLatitude:String = ""
        var CurrentLocationLongitude:String = ""

        if let latitude = USERDEFAULT.value(forKey: "CurrentLocationLatitude") as? Double {
            CurrentLocationLatitude = "\(latitude)"
            //print(myLatitude)
        }

        if let longitude = USERDEFAULT.value(forKey: "CurrentLocationLongitude") as? Double {
            CurrentLocationLongitude = "\(longitude)"
            //print(myLongitude)
        }

        direction = String(format: "http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", CurrentLocationLatitude, CurrentLocationLongitude, latitude, longitude)

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }

    }
    //For SAFARI Browser
    else {
        var direction:String = ""
        direction = String(format: "http://maps.google.com/maps?q=%@,%@", latitude, longitude)
        direction = direction.replacingOccurrences(of: " ", with: "+")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
}

願っています、これがあなたが探しているものです。心配な点は何でも私に連絡してください。:)

「g」が「q」に変わります

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]

それでも問題が解決しない場合は、このビデオで Google から「マイ マップ」を取得して iPhone に表示する方法を説明します。リンクを取得して誰にでも送信すれば、機能します。

http://www.youtube.com/watch?v=Xo5tPjsFBX4

Googleマップへの移動にはこのAPIを使用し、目的地の緯度と経度を送信します

NSString* addr = nil;
     addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", destinationLat,destinationLong];

NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];

Google URL 形式よりもさらに柔軟な機能が必要な場合、または地図アプリを起動する代わりにアプリケーションに地図を埋め込みたい場合は、次の場所に例があります。 https://sourceforge.net/projects/quickconnect.

Google URL 形式よりも柔軟性が必要な場合、または地図アプリを起動する代わりにアプリケーションに地図を埋め込みたい場合 ここに例があります.

すべての埋め込みを行うためのソース コードも提供されます。

iPhone4 iOS 6.0.1 (10A523)

Safari と Chrome の両方に対応。どちらも現在(2013年6月10日)までの最新バージョンです。

以下の URL スキームも機能します。ただし、Chromeの場合はページ内でのみ機能し、アドレスバーからは機能しません。

マップ:q=与えられたタイトル@緯度、経度

**Getting Directions between 2 locations**

        NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=%@,%@", @"30.7046", @"76.7179", @"30.4414", @"76.1617"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];

Swift 4 と同様に動作するコード:

ステップ 1 - 以下を info.plist に追加します

<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>

ステップ 2 - 次のコードを使用して Google マップを表示します

    let destinationLatitude = "40.7128"
    let destinationLongitude = "74.0060"

    if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
        if let url = URL(string: "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }else{
        if let url = URL(string: "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top