2011-11-26 52 views
2

嗨,我有一個使用地圖和位置的iPhone應用程序。在Xcode中打開本地谷歌地圖

我希望用戶能夠按下一個按鈕,該按鈕可以通過轉向指向該位置。我瞭解我不允許在應用中執行此操作,但我想知道是否有人可以告訴我是否可以創建指向本地Google地圖應用的鏈接,該應用將從用戶當前位置輸入路線指向位置。

如果有可能你能告訴我怎麼做?

任何幫助,將不勝感激。

感謝

回答

10

當然,這是可能的 - 你剛剛告訴系統打開谷歌地圖的鏈接,與起始和結束地址設置參數。

您可能需要使用下面的谷歌地圖URL嘗試:

http://maps.google.com/maps?saddr=x,y&daddr=x,y

所以,你可以看到這兩個參數是saddr(起始地址)和 daddr(目的地址)。您將這些設置爲一對座標,並用逗號分隔。

這是我寫的一段代碼,它會將用戶從當前位置移動到特定位置(在我的情況下是硬編碼的)。

這是核心位置委託方法,一旦它們的位置建立,它就會被調用。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    if (newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy) { 
     [self.locationManager stopUpdatingLocation]; 
     CLLocationCoordinate2D coords = newLocation.coordinate; 
     NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%g,%g&daddr=50.967222,-2.153611", coords.latitude, coords.longitude]; 
     NSURL *url = [NSURL URLWithString:stringURL]; 
     [[UIApplication sharedApplication] openURL:url]; 
    } 
} 

要設置這一切了,你可以當你想設置它(說在viewDidLoad)intialise像這樣一個位置管理器的屬性添加到您的控制器,然後:

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
[self.locationManager startUpdatingLocation]; 

調用[[UIApplication sharedApplication] openUrl:url];會將其發送到系統的URL處理程序,該程序會檢測到它是谷歌地圖鏈接,並在地圖中打開它。

希望可以幫到