我想将城市名称发送到服务器。我使用 CLLocationManager 获取纬度经度。然后我使用此链接进行反向地理编码。
https://maps.googleapis.com/maps/api/geocode/json?latlng=lati,longi&key=myApiKey
我的问题是对于不同的位置,地址组件的数量是不同的。例如,我正在获取当前位置的地址组件数组。
"results": [
{
"address_components": [
{
"long_name": "ABC Rd",
"short_name": "ABC Rd",
"types": [
"route"
]
},
{
"long_name": "My City",
"short_name": "My City",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "My Province",
"short_name": "AB",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "My Country",
"short_name": "MC",
"types": [
"country",
"political"
]
}
],
对于我客户的位置,我得到了这个
results": [
{
"address_components": [
{
"long_name": "4",
"short_name": "4",
"types": [
"street_number"
]
},
{
"long_name": "some name",
"short_name": "some name",
"types": [
"route"
]
},
{
"long_name": "some name",
"short_name": "Some name",
"types": [
"political",
"sublocality",
"sublocality_level_2"
]
},
{
"long_name": "some name",
"short_name": "some name",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "city",
"short_name": "city",
"types": [
"locality",
"political"
]
},
{
"long_name": "some name",
"short_name": "Some name",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Client country",
"short_name": "CC",
"types": [
"country",
"political"
]
},
{
"long_name": "12345",
"short_name": "12345",
"types": [
"postal_code"
]
}
],
本地址组成部分不同时,如何获取不同位置的确切城市名称。首先,我尝试获取组件索引号,但由于组件数量不同,我无法做到这一点。正确的方法是什么?请帮我。 谢谢
更新
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks objectAtIndex:0];
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(@"----LOCATION NAME----%@",[placemark.addressDictionary valueForKey:@"Name"]);
NSLog(@"-----STREET ADDRESS---%@",[placemark.addressDictionary valueForKey:@"Thoroughfare"]);
NSLog(@"-----CITY-----%@",[placemark.addressDictionary valueForKey:@"City"]);
strCountry=placemark.country;
NSLog(@"Address------%@",address);
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
我得到的结果
----LOCATION NAME----My Rd
-----STREET ADDRESS---My Rd
-----CITY-----(null)
Address------(null) My Rd
(null) (null)
(null)
My Country
这就是我调用位置更新的方式
-(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];
}
最佳答案
来自 Google API 的 json 响应可以包含不同的位置标记,具体取决于位置。使用索引不是正确的方法。您可以在 type 为 locality 的 json 组件中找到城市名称。下面是代码片段
NSDictionary *locationData = [[json objectForKey:@"results"] objectAtIndex:0];
NSArray* addressComponents= [locationData objectForKey:@"address_components"];
//Iterate each result of address components - find locality and country
NSString *cityName;
for (NSDictionary* address in addressComponents)
{
NSArray* addressType = [address objectForKey:@"types"];
NSString* firstType = [addressType objectAtIndex:0];
if([firstType isEqualToString:@"locality"])
cityName = [address objectForKey:@"long_name"];
}
或者您也可以在 iOS 中使用 CLGeocoder API。
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:lat longitude:long];
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark %@",placemark.locality); // Get the city name
}];
关于ios - 如何在 iOS 中获取当前城市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42478918/