2010-10-07 82 views
0

一直在尋找這一點,但我似乎無法理解如何讓用戶當前的位置變成Google地圖的URL,以打開Goog​​le地圖應用程序,並填充指示。在Google地圖openURL中使用位置管理器中的當前位置?

我已經從另一個教程中獲取這段代碼並將其添加到我的應用中。我需要從下面的「開始」lat/lng進入我的IBAction以打開Goog​​le地圖應用程序。

- (void)locationUpdate:(CLLocation *)location { 
    // Lat/Lng of user (Needs to be sent to IBAction for btnDirections) 
    CLLocationCoordinate2D start = { location.coordinate.latitude, location.coordinate.longitude }; 
} 

以下是我的「獲取路線」按鈕的IBAction。

- (IBAction) onButtonClick:(id) sender { 
    if(sender == btnDirections) { 
     CLLocationCoordinate2D destination = { 52.48306771095311, -1.8935537338256836 };    
     NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", 
             newLocation.coordinate.latitude, newLocation.coordinate.longitude, destination.latitude, destination.longitude]; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];  
    } 
} 

我已經在另一個應用程序之前這樣做,但這種觀點包含顯示這讓我得到緯度/經度容易的用戶位置的地圖。

在此先感謝。

回答

0

在我的代碼,我有格式的URL:

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

(這就是 '地圖' 從你的加入)。

除此之外,您可以設置您的位置請求在設置精度後停止,您是否爲您的目的設置了足夠精確的位置?

哦,最後一點,獲取當前位置可能無法在模擬器中工作。

+0

位置是在我的模擬器的工作。位置更新背後的想法是,如果人們朝着/遠離目的地的方向行駛,它會打開Goog​​le地圖,並在最近的位置不到一分鐘前發出指示。 – 2010-10-07 15:36:31

0

您需要使用戶的當前位置可用於onButtonClick:方法。您正在創建每個位置更新的開始結構,但您永遠不會在該方法之外提供它。

1

只需通過「當前+位置」作爲參數傳遞給谷歌地圖應用程序:

http://maps.google.com/maps?saddr=Current+Location&daddr=Heathrow+Airport+London 
+0

它不適用於英語以外的設備語言,唉。 – 2011-04-28 22:00:40

+0

或在iPad上,即使是英文 – 2011-05-05 11:17:49