8

ここで奇妙な問題があります。私のアプリは、iOS (5.1 と 6 の両方) に組み込まれているマップを呼び出せるはずです。iOS6 では問題なく動作しますが、iOS5.1 では動作しません。iOS6のマップが呼び出され、saddrからdaddrへの方向がトレースされますが、iOS5の場合、マップアプリが呼び出されますが、daddrにピンが1つだけ配置されます。なんらかの理由で初期座標 (saddr) が表示されず、方向が追跡されません。

これが私のコードです:

addr = [NSString stringWithFormat: @"maps://saddr=%f,%f&daddr=%f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude, oldLatitude, oldLongitude];
NSURL *url = [NSURL URLWithString:addr];
[[UIApplication sharedApplication] openURL:url];

URL を「http://maps.google.com/something」に変更しようとしましたが、組み込みのマップ アプリではなく Safari を呼び出します。変数が URL に適切に渡されていることに気付きました。

何か案は?

前もって感謝します!

4

2 に答える 2

35

同様の問題が発生し、Googleマップアプリケーションが削除されたという事実に対処するために、いくつかの条件付きOSコードを作成する必要がありました。新しいMKMapItemリファレンスから

//first create latitude longitude object
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude,longitude);

//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placeMark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    //using iOS6 native maps app
    [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];        
} 
else
{
    //using iOS 5 which has the Google Maps application
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", latitude, longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

[placeMark release];
[destination release];

徒歩ルートを取得するには:

  1. iOS6マップの場合-MKLaunchOptionsDirectionsModeWalking代わりに設定できますMKLaunchOptionsDirectionsModeDriving
  2. Googleマップ&dirflg=wの場合-URLに追加します。

iOS6ではopenInMapsWithLaunchOptionsを使用する方が良いと思います。これにより、マップアプリケーションの応答方法を完全に制御できるようになります。

于 2012-09-14T21:57:57.127 に答える
0

とを使用MKPlacemarkして、マップ ピンの座標タイトルのMKMapItem両方でマップ アプリを起動できます。

NSString *pinTitle;
CLLocationCoordinate2D coordinate;

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:@{(id)kABPersonAddressStreetKey: pinTitle}];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];

if ([mapItem respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    [mapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving}];
}
else
{
    // Google Maps fallback
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=Current+Location", locationItem.coordinate.latitude, locationItem.coordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}

定数を利用するには、リンクする必要があり、コードのどこかにAddressBook.framework追加する必要があることに注意してください。#import <AddressBook/AddressBook.h>kABPersonAddressStreetKey

于 2012-11-20T01:38:26.790 に答える