简体   繁体   English

IOS从lat / long获取CLLocationManager的位置

[英]IOS get location with CLLocationManager from lat/long

I am trying to Reverse geocode location from Lat/Long value that I get earlier in the App and I would like from this coordinate to find the city name, country name and ISO. 我试图从我之前在应用程序中获得的Lat / Long值反转地理编码位置,我希望从此坐标中找到城市名称,国家/地区名称和ISO。

I am currently using CLLocationManager to get actual location information with the folowing code: 我目前正在使用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];

 }];

It is working perfectly but, It is possible to do the same from Long/Lat value that I had already saved in the device and not with the current location like on the actual code ? 它工作得很好但是,可以从我已经保存在设备中的Long / Lat值中做同样的事情,而不是像实际代码那样使用当前位置吗?

Update and solution: 更新和解决方案:

Thanks Mark for the answer, I finally use the following code to get info from saved coordinate: 感谢Mark的回答,我终于使用以下代码从保存的坐标中获取信息:

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

 }];

Yes. 是。 Create a CLLocation object using the initWithLatitude:longitude: method using your saved lat/lon values, and pass that to reverseGeocodeLocation: . 使用initWithLatitude:longitude:方法使用保存的lat / lon值创建一个CLLocation对象,并将其传递给reverseGeocodeLocation: .

I am surprised that you say this is working (although, if you're on the simulator, location services are simulated anyway, which might be the reason) because when you call startUpdatingLocation , your implementation of CLLocationManagerDelegate methods like locationManager:didUpdateToLocation:fromLocation: get called. 我很惊讶您说这是有效的(但是,如果您在模拟器上,无论如何都会模拟位置服务,这可能是原因),因为当您调用startUpdatingLocation ,您实现CLLocationManagerDelegate方法,如locationManager:didUpdateToLocation:fromLocation:被叫。 (You've implemented these right?) It is only when this (and other) delegate method is called that you can be certain that you have successfully determined the user's location. (您已经实现了这些权利吗?)只有在调用此(和其他)委托方法时,您才可以确定已成功确定用户的位置。

You may want to read up on the CLLocationManagerDelegate protocol and on Location Services best practices as documented by Apple. 您可能希望阅读Apple记录的CLLocationManagerDelegate协议和位置服务最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM