2012-09-06 50 views
0

我想扭轉地理編碼位置的緯度/長度值,我得到的應用程序,我想從這個座標來找到城市名稱,國家名稱和ISO。IOS獲取位置與CLLocationManager從經緯度

我目前使用CLLocationManager得到與如下因素代碼的實際位置信息:

//Auto geolocation and find city/country 
locationManager.delegate=self; 

//Get user location 
[locationManager startUpdatingLocation]; 
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: 
^(NSArray *placemarks, NSError *error) { 

    //Get nearby address 
    CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

    //String to hold address 
    locatedAtcountry = placemark.country; 
    locatedAtcity = placemark.locality; 
    locatedAtisocountry = placemark.ISOcountryCode; 

    //Print the location to console 
    NSLog(@"Estas en %@",locatedAtcountry); 
    NSLog(@"Estas en %@",locatedAtcity); 
    NSLog(@"Estas en %@",locatedAtisocountry); 

    [cityLabel setText:[NSString stringWithFormat:@"%@,",locatedAtcity]]; 
    [locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]]; 

    //Set the label text to current location 
    //[locationLabel setText:locatedAt]; 

}]; 

它可以正常使用,但是,這是可以做到的,我已經保存在朗/緯度值相同該設備,而不是像當前位置上的實際代碼?

更新和解決方案:

感謝馬克的答案,我終於可以用下面的代碼來獲取信息,從保存的座標:

CLLocation *location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700]; 

[self.geoCoder reverseGeocodeLocation: location completionHandler: 
^(NSArray *placemarks, NSError *error) { 

    //Get nearby address 
    CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

    //String to hold address 
    locatedAtcountry = placemark.country; 
    locatedAtcity = placemark.locality; 
    locatedAtisocountry = placemark.ISOcountryCode; 

    //Print the location to console 
    NSLog(@"Estas en %@",locatedAtcountry); 
    NSLog(@"Estas en %@",locatedAtcity); 
    NSLog(@"Estas en %@",locatedAtisocountry); 

    [cityLabel setText:[NSString stringWithFormat:@"%@",locatedAtcity]]; 
    [locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]]; 

    //Set the label text to current location 
    //[locationLabel setText:locatedAt]; 

}]; 

回答

2

是。使用保存的緯度/經度值使用initWithLatitude:longitude:方法創建一個CLLocation對象,並將其傳遞給reverseGeocodeLocation:。因爲當你打電話給startUpdatingLocation時,你的CLLocationManagerDelegate方法的實現得到了,如locationManager:didUpdateToLocation:fromLocation:得到的結果就是這樣的結果(如果你在模擬器上,那麼位置服務是模擬的)調用。 (你已經實現了這些權利?)只有當這個(和其他)委託方法被調用時,你才能確定你已經成功地確定了用戶的位置。

您可能想了解CLLocationManagerDelegate協議和Location Services最佳實踐,如Apple所述。