2011-08-18 127 views
0

Iam開發一個應用程序。我使用google 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.latitude和newlocation.Coordinates.longitude傳遞給該方法的URL。

與其他人的建議我改變了下面的代碼。

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

    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]; 
} 

這也給像Exe_Bad_Access錯誤時的第一行executed.SO請告訴我如何寫一個。

+0

newLocation.lattitude應該newLocation.latitude – kviksilver

回答

4
-(void)getLocation{ 
    locationManager = [[CLLocationManager alloc] init] ; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 

} 


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

    NSString * lat = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.latitude]autorelease]; 
    NSString * lon = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.longitude]autorelease]; 


    [locationManager stopUpdatingLocation]; 

    //set google maps locations 
    NSString * googleString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@+%@",lat,lon]; 


} 
+0

但是,如果你給該字符串NSURL對象,顯示空值。 –

+0

非常感謝你 – SampathKumar

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

這將工作:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 
    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.coordinate.latitude, newLocation.coordinate.longitude]; 
    NSURL *url = [NSURL URLFromString:address]; 
}