我正在获得纬度经度并在ios中进行反向地理编码。这是我到目前为止所做的。
-(void)GetLocationData
{
if (self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
else
{
nil;
}
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
else
{
nil;
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;//kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
NSString *longitude;
NSString *latitude;
if (currentLocation != nil) {
longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];//
latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];//
NSLog(@"Longitude-----%@-----Latitude----%@",longitude,latitude);
dm.strLatitude=latitude;
dm.strLongitude=longitude;
}
[self.locationManager stopUpdatingLocation];
geocoder = [[CLGeocoder alloc] init];
// Reverse Geocoding
NSLog(@"Resolving the Address");
CLLocationDegrees degreesLati=lati;
CLLocationDegrees degreesLongi=longi;
CLLocation *loc=[[CLLocation alloc] initWithLatitude:degreesLati longitude:degreesLongi];
[geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks objectAtIndex:[placemarks count]-1];
NSString *address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.subLocality,
placemark.subAdministrativeArea,
placemark.country];
// NSString *address=[self.placemark];
NSDictionary *dictAddress = [NSDictionary dictionaryWithDictionary:placemark.addressDictionary];
NSMutableDictionary *dictTxtData = [NSMutableDictionary dictionary];
NSLog(@"country:%@",dictAddress);
strCountry=placemark.country;
NSLog(@"Address------%@",address);
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
但我的问题是在Android和iOS中为此返回2个不同的地址。但Android地址是正确的。这就是Android应用获取地址的方式。
{
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try {
/**
* Geocoder.getFromLocation - Returns an array of Addresses
* that are known to describe the area immediately surrounding the given latitude and longitude.
*/
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, this.geocoderMaxResults);
return addresses;
} catch (IOException e) {
//e.printStackTrace();
Log.e(TAG, "Impossible to connect to Geocoder", e);
}
}
更新
反向地理编码
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks objectAtIndex:[placemarks count]-1];
NSString *address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.subLocality,
placemark.subAdministrativeArea,
placemark.country];
// NSString *address=[self.placemark];
NSDictionary *dictAddress = [NSDictionary dictionaryWithDictionary:placemark.addressDictionary];
NSMutableDictionary *dictTxtData = [NSMutableDictionary dictionary];
strCountry=placemark.country;
NSLog(@"Address------%@",address);
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
我以这种方式currentLocation
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
答案 0 :(得分:0)
我想猜一下!!
那些lati
和longi
是什么?不应该使用longitude
和longitude
来代替
CLLocationDegrees degreesLati = currentLocation.coordinate.longitude;
CLLocationDegrees degreesLongi = currentLocation.coordinate.latitude;
代替:
CLLocationDegrees degreesLati=lati;
CLLocationDegrees degreesLongi=longi;