2014-02-09 24 views
0

我想打開谷歌地圖通過我的應用程序來獲取方向與源地址等於我的當前位置和目標地址是我的string.I使用了下面的代碼,我是能夠打開應用程序,但無法獲取方向。通過在iPhone Safari中的應用程序打開谷歌地圖

NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=My+Location&daddr=%@", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 

通過使用這些代碼行,我可以打開應用程序,但地圖沒有取當前位置,目標地址在那裏。

我已經通過傳遞當前位置的經緯度來使用這個,但是這也顯示了當前位置經緯度和長到0.0000。

NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%1.6f,%1.6f&daddr=%@", 
         coordinate.latitude, coordinate.longitude, 
         [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
     [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 

回答

2

這爲我工作:

NSString *coordinateString1 = [NSString stringWithFormat:@"%f,%f", coordinate1.latitude, coordinate1.longitude]; 
NSString *coordinateString2 = [NSString stringWithFormat:@"%f,%f", coordinate2.latitude, coordinate2.longitude]; 
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",coordinateString1,coordinateString2]]; 
[[UIApplication sharedApplication] openURL: URL]; 
+0

我想表明通過當前所在位置到指定的地址字符串。 – ankyy

+0

更新了我的答案。 – Gad

+0

@ Daij-Djan你是指蘋果地圖?我在我的手機上運行它並查看谷歌地圖(地址是maps.google.com) – Gad

相關問題