159

iOS 6 より前のバージョンでは、次のような URL を開くと (Google) マップ アプリが開きます。

NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];

新しい Apple Maps の実装により、Mobile Safari から Google Maps を開くだけです。iOS 6 で同じ動作を実現するにはどうすればよいですか? マップ アプリをプログラムで開き、特定の場所/住所/検索などを指すようにするにはどうすればよいですか?

4

12 に答える 12

281

これが公式のAppleのやり方です:

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
{
    // Create an MKMapItem to pass to the Maps app
    CLLocationCoordinate2D coordinate = 
                CLLocationCoordinate2DMake(16.775, -3.009);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate 
                                            addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"My Place"];
    // Pass the map item to the Maps app
    [mapItem openInMapsWithLaunchOptions:nil];
}

その場所への運転や徒歩の指示を取得したい場合は、の配列にmapItemForCurrentLocationを含めて、起動オプションを適切に設定できます。MKMapItem+openMapsWithItems:launchOptions:

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
{
    // Create an MKMapItem to pass to the Maps app
    CLLocationCoordinate2D coordinate = 
                CLLocationCoordinate2DMake(16.775, -3.009);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate 
                                            addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"My Place"];

    // Set the directions mode to "Walking"
    // Can use MKLaunchOptionsDirectionsModeDriving instead
    NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
    // Get the "Current User Location" MKMapItem
    MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
    // Pass the current location and destination map items to the Maps app
    // Set the direction mode in the launchOptions dictionary
    [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] 
                    launchOptions:launchOptions];
}

elseその後のステートメントで、元のiOS5以下のコードを保持できますif。配列内のアイテムの順序を逆にするとopenMapsWithItems:、座標から現在の場所への方向が取得されることに注意してください。おそらくMKMapItem、現在のロケーションマップアイテムの代わりに構築されたものを渡すことで、任意の2つのロケーション間の方向を取得するために使用できます。私はそれを試していません。

最後に、ルート案内が必要な住所(文字列として)がある場合は、ジオコーダーを使用して、を使用してを作成しMKPlacemarkますCLPlacemark

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"Piccadilly Circus, London, UK" 
        completionHandler:^(NSArray *placemarks, NSError *error) {

        // Convert the CLPlacemark to an MKPlacemark
        // Note: There's no error checking for a failed geocode
        CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
        MKPlacemark *placemark = [[MKPlacemark alloc]
                                  initWithCoordinate:geocodedPlacemark.location.coordinate
                                  addressDictionary:geocodedPlacemark.addressDictionary];

        // Create a map item for the geocoded address to pass to Maps app
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem setName:geocodedPlacemark.name];

        // Set the directions mode to "Driving"
        // Can use MKLaunchOptionsDirectionsModeWalking instead
        NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};

        // Get the "Current User Location" MKMapItem
        MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];

        // Pass the current location and destination map items to the Maps app
        // Set the direction mode in the launchOptions dictionary
        [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];

    }];
}
于 2012-10-15T17:10:35.707 に答える
80

私自身の質問に対する答えを見つけました。Appleは、マップのURL形式をここに文書化しています。maps.google.com基本的に。に置き換えることができるようですmaps.apple.com

更新: iOS6のMobileSafariでも同じことがわかります。リンクをタップすると、以前のバージョンhttp://maps.apple.com/?q=...と同じように、その検索でマップアプリが開きます。http://maps.google.com/?q=...これは機能し、上記のリンク先のページに記載されています。

更新:これは、URL形式に関する私の質問に答えます。しかし、ここでのnevan kingの答え(以下を参照)は、実際のMapsAPIの優れた要約です。

于 2012-09-19T23:38:26.333 に答える
41

これを行う最善の方法は、新しい iOS 6 メソッドを呼び出すことです。MKMapItem openInMapsWithLaunchOptions:launchOptions

例:

CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(40.446947, -102.047607);
MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil];
MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation];

NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey];

[endingItem openInMapsWithLaunchOptions:launchOptions];

これにより、現在地からの運転のためのナビゲーションが開始されます。

于 2012-09-22T15:05:11.313 に答える
7

あなたがmaps.apple.comのURL「scheme」を見つけたようです。古いデバイスをmaps.google.comに自動的にリダイレクトするため、これは良い選択です。しかし、iOS 6の場合、利用したい新しいクラスがあります:MKMapItem

あなたが興味を持っている2つの方法:

  1. -openInMapsWithLaunchOptions: -MKMapItemインスタンスで呼び出して、Maps.appで開きます。
  2. + openMapsWithItems:launchOptions: -MKMapItemクラスで呼び出して、MKMapItemインスタンスの配列を開きます。
于 2012-09-19T23:43:48.770 に答える
4

http://maps.apple.com?q= ... リンク設定を使用すると、古いデバイスで最初にサファリ ブラウザが開くのが面倒でした。

iOS 5 デバイスで maps.apple.com への参照を使用してアプリを開く場合、手順は次のようになります。

  1. アプリ内の何かをクリックすると、maps.apple.com の URL が参照されます
  2. サファリはリンクを開く
  3. maps.apple.com サーバーは maps.google.com の URL にリダイレクトします
  4. maps.google.com の URL が解釈され、Google マップ アプリが開きます。

(非常に明白で紛らわしい) ステップ 2 と 3 はユーザーにとって煩わしいと思います。そのため、OS のバージョンを確認し、デバイスで maps.google.com または maps.apple.com を実行します (それぞれ ios 5 または ios 6 OS バージョンの場合)。

于 2012-09-27T10:46:28.873 に答える
3

URL を起動する前に、URL から特殊文字をすべて削除し、スペースを + に置き換えます。これにより、いくつかの頭痛の種が解消されます。

    NSString *mapURLStr = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@",@"Limmattalstrasse 170, 8049 Zürich"];

    mapURLStr = [mapURLStr stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[mapURLStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    if ([[UIApplication sharedApplication] canOpenURL:url]){
            [[UIApplication sharedApplication] openURL:url];
        }
于 2016-07-07T10:41:55.073 に答える
3

代わりに Google マップを開きたい (または 2 つ目のオプションとして提供したい) 場合は、ここにcomgooglemaps://記載されているおよびcomgooglemaps-x-callback://URL スキームを使用できます。

于 2015-01-20T16:51:51.653 に答える
3

この問題に関する私の調査により、次の結論に至りました。

  1. maps.google.com を使用すると、ios ごとにサファリでマップが開きます。
  2. maps.apple.comを使用すると、ios 6のマップアプリケーションでマップが開き、ios 5でもうまく機能し、ios 5ではサファリで通常どおりマップを開きます。
于 2012-10-01T07:57:18.013 に答える
2
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@"
                             ,[dataDictionary objectForKey:@"practice_address"]
                             ,[dataDictionary objectForKey:@"practice_city"]
                             ,[dataDictionary objectForKey:@"practice_state"]
                             ,[dataDictionary objectForKey:@"practice_zipcode"]];


        NSString *mapAddress = [@"http://maps.apple.com/?q=" stringByAppendingString:[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSLog(@"Map Address %@",mapAddress);

        [objSpineCustomProtocol setUserDefaults:mapAddress :@"webSiteToLoad"];

        [self performSegueWithIdentifier: @"provider_to_web_loader_segue" sender: self];

//VKJ

于 2013-11-08T06:40:19.140 に答える
2

マップを使用せず、プログラムで UiButton アクションを使用するだけで、これはうまく機能しました。

// Button triggers the map to be presented.

@IBAction func toMapButton(sender: AnyObject) {

//Empty container for the value

var addressToLinkTo = ""

//Fill the container with an address

self.addressToLinkTo = "http://maps.apple.com/?q=111 Some place drive, Oak Ridge TN 37830"

self.addressToLinkTo = self.addressToLinkTo.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

let url = NSURL(string: self.addressToLinkTo)
UIApplication.sharedApplication().openURL(url!)

                }

このコードの一部を少し広げることができます。たとえば、変数をクラス レベルの変数として配置し、別の関数でその変数を埋めてから、ボタンを押すと変数の内容が取得され、URL で使用されるようにスクラブされました。

于 2015-05-04T20:14:54.747 に答える