2010-04-05 141 views
0

我有一個啓動谷歌地圖應用程序的應用程序。代碼是:CoreLocation當前位置

UIApplication *app = [UIApplication sharedApplication]; 
[app openURL:[[NSURL alloc] initWithString: @"http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&saddr="]]; 

saddr =應該是當前位置。我得到的當前位置與

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

NSLog(@"%f,%f", [newLocation coordinate]); 

日誌顯示正確的座標一樣

2010-04-05 15:33:25.436 deBordeaux[60657:207] 37.331689,-122.030731 

我沒有找到傳送座標到URL字符串的正確途徑。有人可以給我一個提示嗎?


嗯,我在我的.h

入口在我的方法我用「newLocation」,而不是你的「位置」。的代碼是:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 

NSLog(@"%f,%f", [newLocation coordinate]); 
NSLog(@"%f", [newLocation coordinate].latitude); 

storedLocation = [newLocation coordinate]; 

NSLog(@"Standort neu String: %@", storedLocation); 

作爲結果獲得:

  • 2010-04-05 20:28:44.397 deBordeaux [64179:207] 37.331689,-122.030731
  • 2010-04-05 20:28:44.398 deBordeaux [64179:207] 37.331689
  • 編程接收信號:「EXC_BAD_ACCESS」。

回答

0

你不應該使用地址參數的URL,但LL

你應該通過緯度和經度

http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&ll=37.331689,-122.030731 

,你可以從CLocation獲得他們throught

[location coordinate].latitude 
[location coordinate].longitude 

編輯: 首先,你可以直接存儲類型的對象CLLocationCoordinate2D,然後在需要時將其轉換爲NSString:

// in your .h 
CLLocationCoordinate2D storedLocation; 

// in your method 
storedLocation = [location coordinate]; 

// when you want to ask google maps 
[NSString stringWithFormat:@"http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&ll=%f,%f", storedLocation.latitude, storedLocation.longitude]; 

記住,CLLocationCoordinate2D被定義爲

typedef struct { 
    CLLocationDegrees latitude; 
    CLLocationDegrees longitude; 
} CLLocationCoordinate2D; 

,你必須typedef double CLLocationDegrees

+0

感謝您的快速答覆。我瞭解「ll =」的用法,而不是「saddr =」。我的問題是,UIApplication ....的代碼位於 - (IBAction)nav ...中,後面的位置座標位於#pragma標記CLLocationManagerDelegate中。我找不到在NSString中「存儲」位置座標的方法。 – Christian 2010-04-05 17:45:52