2011-08-18 17 views
0

Iam開發一個應用程序。我使用谷歌API獲取位置信息。爲此,我使用CLLocationManager獲取位置經緯度值。而我的pblm是如何將這些經度和緯度值傳遞給nsurl。和iam直接給一個位置值給下面的url。如何將經緯度值傳遞給ns url?

NSURL *URL = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search   /json?location=40.7143528,-74.0059731&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM"]; 

我在下面的方法中寫下這個函數來獲取和傳遞經緯度值。

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

所以,請告訴我如何將newlocation.Coordinates.latitudenewlocation.Coordinates.longitude傳遞給該方法的URL。或者有什麼方法可以通過其他方法傳遞這些值。

回答

2

只要使用這個..

NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM",newLocation.latitude, newLocation.longitude]; 
NSURL *URL = [NSURL URLWithString:url]; 
+0

一個小錯字。 https :) –

+0

oopse .. done .. :) 謝謝。 – Hadi

+0

但是,當第一行執行 –

1

這應該工作,但是是大腦編譯:)

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
//Filter the location first here, it could be an invalid location or maybe too recent to the last one because this method is called several times in short times of periods. 

//Then use the value 
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f, %f&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM", newLocation.lattitude, newLocation.longitude]; 
NSURL *url = [NSURL URLFromString:address]; 
//do your thing here... 

} 

我希望它能幫助

+0

但是當執行第一行時會出現錯誤,例如執行錯誤的訪問錯誤 –

0

試試這個,

NSURL *addressUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@,%@&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM",newLocation.latitude, newLocation.longitude, nil]]; 
+0

我得到了「請求成員」緯度「的錯誤,而不是結構或聯合」 –

+0

用您的Lattitude值和newLocation替換newLocation.latitude 。經度與經度值.. –

+0

但我想通過更新的值 –

1

您可以傳遞經緯度n位置管理器找到Google API的新位置:

NSString *address = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv", newLocation.lattitude, newLocation.longitude]; 
NSURL *url = [NSURL URLFromString:address]; 
相關問題