2012-09-26 25 views
1

開始在iPhone地圖應用我開發的應用程序的IOS 6
我想要運行的地圖應用程序,並通過它開始和目標,所以我可以瀏覽用戶。
如何從目標C

UIApplication *app = [UIApplication sharedApplication]; 

    NSString *coordinates = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", ...]; 

    [app openURL:[NSURL URLWithString: coordinates]]; 

我認爲這個代碼將在瀏覽器中打開模擬器上的谷歌地圖,而地圖應用的設備,但在設備上運行的瀏覽器谷歌地圖。
我做錯了什麼?

回答

7

如果你不知道,蘋果不再使用谷歌地圖,所以你必須使用他們的新URL方案爲蘋果地圖。 (注:如果你支持的iOS 5,那麼你應該同時使用谷歌地圖方案及蘋果地圖)

下面是一個例子查詢http://maps.apple.com/maps?daddr=San+Francisco,+CA&saddr=cupertino

這裏是它的文檔:Apple Maps URL Schemes

2

另一種選擇,如果你有一個MKPlacemark對象:

// placemark is your MKPlacemark object 
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:placemark]; 

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)]) 
{ 
    // Using iOS6 native maps app 
    [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];  
} 
else 
{ 
    // Using iOS5 which has the Google Maps application 
    NSString *currentLocation = @"Current%20Location"; 
    NSString *routeString = [NSString stringWithFormat:@"%@saddr=%@&daddr=%@", kMapsBaseUrl, currentLocation, address.mapAddress]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:routeString]]; 
} 
1
-(void)openAddressOnNativeMapApp{ 
NSString *addressOnMap = @"cupertino"; //place name 
NSString* addr = [NSString stringWithFormat:@"http://maps.apple.com/?q=%@",addressOnMap]; 
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
[[UIApplication sharedApplication] openURL:url]; 

}

獲取更多信息請訪問Apple Doc for open native map App

2

這是我在iOS 8

先用我嘗試打開URL @「comgooglemaps://」,如果它的工作原理,這意味着他們安裝了谷歌地圖應用程序,那麼我可以打開應用程序。

如果不工作,那麼該應用程序是不存在的,在Safari只需打開谷歌地圖。

在這兩種情況下,將查詢q=London

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){ //open google maps app 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?q=London"]]; 
} 
else{ //open browser 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]; 
}