73

openURLパラメータsaddrと場所の文字列または緯度/経度を使用して Google マップの URL を呼び出すことで、iPhone マップ アプリケーションを起動できることはわかっていますdaddr(以下の例を参照)。

しかし、マップアプリの位置処理コードを使用できるように、開始アドレスを「現在の位置」マップのブックマークにすることができるかどうか疑問に思っています。私のGoogle検索はかなり役に立ちませんでした。

例えば:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@", myLatLong, latlong]]];

の代わりに現在の場所のブックマークを呼び出す何かを除いてmyLatLong

4

16 に答える 16

135

iOS 6 より前

現在の場所を取得するには Core Location を使用する必要がありますが、その緯度/経度のペアを使用すると、Maps を使用してそこから住所または場所までルートを表示できます。そのようです:

CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
// this uses an address for the destination.  can use lat/long, too with %f,%f format
NSString* address = @"123 Main St., New York, NY, 10001";
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",
                    currentLocation.latitude, currentLocation.longitude,
                    [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

最後に、CoreLocation を使用して現在の場所を明示的に検索することを避け、@"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"代わりに URLを使用する場合は、 Current+Location文字列をローカライズする方法について、以下のコメントで提供したこのリンクを参照してください。ただし、文書化されていない別の機能を利用しているため、Jason McCreary が以下で指摘しているように、将来のリリースでは確実に機能しない可能性があります。


iOS 6 のアップデート

当初、マップは Google マップを使用していましたが、現在は Apple と Google が別々のマップ アプリを提供しています。

1) Google マップ アプリを使用してルートを指定する場合は、comgooglemaps URL スキームを使用します。

NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving",
                    [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

2) Apple Maps を使用するにはMKMapItem、iOS 6 の新しいクラスを 使用できます。こちらの Apple API ドキュメントを参照してください。

基本的に、宛先座標( latlong)にルーティングする場合は、次のようなものを使用します。

    MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil];
    MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
    destination.name = @"Name Here!";
    NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
    NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 MKLaunchOptionsDirectionsModeDriving, 
                                 MKLaunchOptionsDirectionsModeKey, nil];
    [MKMapItem openMapsWithItems: items launchOptions: options];

同じコードで iOS 6+ と iOS 6 より前の両方をサポートするには、Apple がMKMapItemAPI doc ページに持っている次のようなコードを使用することをお勧めします。

Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
   // iOS 6 MKMapItem available
} else {
   // use pre iOS 6 technique
}

これは、Xcode Base SDK が iOS 6 (または最新の iOS )であることを前提としています。

于 2009-06-08T09:49:53.333 に答える
21
NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=Current Location&saddr=%@",startAddr];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
[url release];

これは、iPhone/iPod の言語が英語に設定されている場合にのみ機能します。他の言語をサポートしたい場合は、ローカライズされた文字列を使用してマップのブックマーク名と一致させる必要があります。

于 2010-12-01T10:05:57.393 に答える
11

これはiPhoneで動作します:

http://maps.google.com/maps?saddr=現在地&daddr=123 Main St,Ottawa,ON

于 2010-11-01T14:46:16.350 に答える
10

次のようなプリプロセッサ #define を使用できます。

#define SYSTEM_VERSION_LESS_THAN(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

iOS のバージョンを理解する。次に、このコードを使用して iOS 6 もサポートできます。

NSString* addr = nil;
if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
   addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
} else {
   addr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
}

NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
于 2012-09-30T09:51:55.743 に答える
5

サンドボックス化のため、マップ アプリケーションのブックマークにアクセスできません。

代わりに、Core Location を使用して現在の場所を自分で決定してください。次に、作成した URL でその場所 (緯度と経度) を使用して、マップを開きます。

于 2009-02-23T15:07:00.867 に答える
4

CMMapLauncherをチェックすることをお勧めします。これは、Apple、Google、および特定のマッピング リクエストで他の iOS マッピング アプリを起動するために構築したミニ ライブラリです。CMMapLauncher を使用すると、質問の方向を取得するコードは次のようになります。

[CMMapLauncher launchMapApp:CMMapAppAppleMaps
          forDirectionsFrom:[CMMapPoint mapPointWithName:@"Origin"
                                              coordinate:myLatLong]
                         to:[CMMapPoint mapPointWithName:@"Destination"
                                              coordinate:latlong]];

ご覧のとおり、iOS 6 とその他の間で必要なバージョン チェックもカプセル化されています。

于 2013-09-07T12:37:29.753 に答える
3

iOS6が出たので!

Apple は悪い意味で注目に値することをした (私の見解では)。

Apple のマップが起動され、iOS 6 を実行しているデバイスのmaps.google.com/?q=場合、iDevice でネイティブ Plan アプリを開く場合は使用しないでください。今はmaps.apple.com/?q=.

開発者が多くの作業を行う必要がないように、フレンドリーな maps.apple.com サーバーが Apple 以外のすべてのデバイスを maps.google.com にリダイレクトするため、変更は透過的です。

このようにして、開発者はすべての Google クエリ文字列を Apple クエリ文字列に切り替えるだけで済みます。これは私がとても嫌いなことです。

今日はその機能を実装しなければならなかったので、実装しました。しかし、モバイル Web サイトにあるすべての URL を Apple のマップ サーバーを対象とするように書き換えるべきではないと感じたので、サーバー側で iDevices を検出し、Apple の URL を提供するだけにしようと考えました。分かち合おうと思いました。

私は PHP を使用しているので、オープンソースの Mobile Detect ライブラリを使用しました: http://code.google.com/p/php-mobile-detect/

擬似メソッドをブール値のゲッターとして使用するだけisiPadで完了です。Google を Apple に変換することはありません ;-)

$server=$detect->isiPad()?"apple":"google";
$href="http://maps.{$server}.com/?q=..."

乾杯!

于 2012-09-21T17:57:01.007 に答える
2

iOS6の場合、アップルのドキュメントでは、同等のmaps.apple.comURLスキームを使用することを推奨しています

だから使用する

http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f

それ以外の

http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f

下位互換性を保つために、コードは次のようになります。

    NSString* versionNum = [[UIDevice currentDevice] systemVersion];
    NSString *nativeMapScheme = @"maps.apple.com";
    if ([versionNum compare:@"6.0" options:NSNumericSearch] == NSOrderedAscending)
        nativeMapScheme = @"maps.google.com";
    }
    NSString* url = [NSString stringWithFormat: @"http://%@/maps?saddr=%f,%f&daddr=%f,%f", nativeMapScheme
             startCoordinate.latitude, startCoordinate.longitude,
             endCoordinate.latitude, endCoordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

Apple Maps URLスキームでサポートされている他のパラメータがたくさんあります:AppleURLスキームリファレンス

コードの他の部分に条件付きコードがある場合は、これらのiOSバージョン検出マクロを使用できます。iOSバージョンのマクロ

于 2012-09-20T10:53:22.373 に答える
2

本当の解決策はここにあります。Z5 コンセプト iOS 開発コード スニペット

少しだけエンコードが必要です。

- (IBAction)directions1_click:(id)sender
{
    NSString* address = @"118 Your Address., City, State, ZIPCODE";
    NSString* currentLocation = @"Current Location";
    NSString* url = [NSStringstringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    UIApplication *app = [UIApplicationsharedApplication];
    [app openURL: [NSURL URLWithString: url]];
}
于 2012-06-02T16:51:22.867 に答える
2

これは、モバイル デバイスの URL からのみ HTML で実行できます。ここにがあります。すばらしいことに、これを QR コードに変換すると、携帯電話でスキャンするだけで、どこにいてもあなたへの道順を知ることができます。

于 2010-05-26T23:34:04.460 に答える
2

場所の許可を求めず、緯度と経度がない場合は、次を使用します。

NSString *destinationAddress = @"Amsterdam";

Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if([placemarks count] > 0) {

            MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];

            MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];

            MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];


            NSArray *mapItems = @[mapItem, mapItem2];

            NSDictionary *options = @{
        MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
        MKLaunchOptionsMapTypeKey:
            [NSNumber numberWithInteger:MKMapTypeStandard],
        MKLaunchOptionsShowsTrafficKey:@YES
            };

            [MKMapItem openMapsWithItems:mapItems launchOptions:options];

        } else {
            //error nothing found
        }
    }];
    return;
} else {

    NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];

    NSString *urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
                 [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                 [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
}

iOS5 の場合、現在の場所は正しい言語である必要があります。この投稿の LocalizedCurrentLocation を使用します http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

iOS6 の場合、CLGeocoder を使用して目印を取得し、それと現在の場所を使用して地図を開きます。

CoreLocation.framework と MapKit.framework を忘れずに追加してください

于 2012-12-06T12:14:54.063 に答える
1

私の提案は、OpenInGoogleMaps-iOSを使用することです。これは最新の選択肢 (2015 年 11 月まで) であり、ココア ポッドのインストールをサポートしており、数回クリックするだけですぐに使用できます。

以下を使用してインストールします。pod "OpenInGoogleMaps"

以下を使用してヘッダーファイルで要求します。#import "OpenInGoogleMapsController.h"

以下のサンプルコード:

/*In case the user does NOT have google maps, then apple maps shall open*/
[OpenInGoogleMapsController sharedInstance].fallbackStrategy = kGoogleMapsFallbackAppleMaps;    

/*Set the coordinates*/
GoogleMapDefinition *definition = [[GoogleMapDefinition alloc] init];   
CLLocationCoordinate2D metsovoMuseumCoords;     //coordinates struct
metsovoMuseumCoords.latitude = 39.770598;
metsovoMuseumCoords.longitude = 21.183215;
definition.zoomLevel = 20;
definition.center = metsovoMuseumCoords;        //this will be the center of the map

/*and here we open the map*/
[[OpenInGoogleMapsController sharedInstance] openMap:definition];       
于 2015-11-10T13:35:17.977 に答える
1

iOS6 Maps アプリの場合は、上記と同じ URL を使用できます http://maps.google.com/maps?saddr=%f,%f&daddr=%@

ただし、Google マップの URL の代わりに URL を使用するとmaps://、次の URL になります。maps://saddr=%f,%f&daddr=%@.

「現在地」を使用してもうまくいかないようなので、座標のままにしました。

もう 1 つの良い点は、下位互換性があることです。iOS5 では、Google マップ アプリを起動します。

于 2012-09-13T12:21:47.120 に答える
0

私は別のスレッドでこれに答えました。(現在の場所は Apple Maps IOS 6 では機能しません)。最初に現在地の座標を取得し、それを使用して地図の URL を作成する必要があります。

于 2013-01-11T05:37:18.433 に答える