【发布时间】:2017-07-17 15:54:50
【问题描述】:
我想将城市名称发送到服务器。我正在使用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 还是 Apple 解决方案可以?
-
我使用google api进行反向地理编码
-
是的,我看到了,但你有使用它吗?
-
我尝试使用 CLGeocoder 但它没有返回任何城市。大多数属性都为 null 这就是我使用 google api 的原因
标签: ios google-maps-api-3 cllocationmanager