[英]how to get city name via CLlocationManager using latitude and longitude in IOS?
I am trying to get address via CLLocationManager using latitude and longitude but it only returns state and country name, I want city also, I am attaching my code can anybody tell me that how can I alter my code to get city name also.Below is my code 我试图通过CLLocationManager使用纬度和经度获取地址,但它只返回州和国家名称,我也想要城市,我附加我的代码可以任何人告诉我,我怎么能改变我的代码以获得城市名称.Below是我的代码
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
currentLocation = [locations objectAtIndex:0];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
//CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:26.9260 longitude:75.8235];
CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
[geocoder1 reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Geocode failed with error: %@", error);
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSLog(@"%@ ", addressDictionary);
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSLog(@"%@ %@ %@ %@", address,city, state, zip);
}
}];
}
Try this .. 试试这个 ..
-(NSString*)getAddressFromLatLong : (NSString *)latLng {
// NSString *string = [[Address.text stringByAppendingFormat:@"+%@",cityTxt.text] stringByAppendingFormat:@"+%@",addressText];
NSString *esc_addr = [latLng stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
NSMutableDictionary *data = [NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *dataArray = (NSMutableArray *)[data valueForKey:@"results" ];
if (dataArray.count == 0) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter a valid address" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}else{
for (id firstTime in dataArray) {
NSString *jsonStr1 = [firstTime valueForKey:@"formatted_address"];
return jsonStr1;
}
}
return nil;
}
Send comma separated Latitude longitude .. and try It's working fine 发送逗号分隔纬度经度..并尝试它的工作正常
Reference Link Get Address From Latitude Longitude using Apple Function in iOS & iPad 参考链接使用iOS和iPad中的Apple功能从纬度经度获取地址
You are using k-constants from ABPerson class from AddressBook framework for CLGeocoder object keys. 您正在使用来自AddressBook框架的ABPerson类的k常量来获取CLGeocoder对象键。 While some may match, I'm pretty sure others will not. 虽然有些人可能会匹配,但我很确定其他人不会。
You can try ... 你可以试试 ...
NSMutableArray *addrArray = [[NSMutableArray alloc] init];
//to return address line(s)
addrArray = [NSMutableArray arrayWithArray: [p.addressDictionary objectForKey:@"FormattedAddressLines"]];
[addrArray removeLastObject]; //country name
NSString *addressString = [addrArray componentsJoinedByString:@" ,"];
#if DEBUG
NSLog(@"Address: %@", addressString);
#endif
We will get Complete Address based on latitude and longitude.Here is the Sample Code. 我们将根据纬度和经度获得完整地址 。这是示例代码。
- (void) getAddressFromLatLon:(CLLocation *)location
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *array, NSError *error){
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
CLPlacemark *placemark = [array objectAtIndex:0];
NSLog(@"City %@",placemark.subAdministrativeArea);
NSLog(@"State %@",placemark.administrativeArea);
}];
}
In below i mentioned different types.So easily you can check it.These are all belongs to CLPlacemark class objects.This class can be available on CoreLocation Framework. 在下面我提到了不同的类型。很容易就可以检查它们。这些都属于CLPlacemark类对象。这个类可以在CoreLocation Framework上使用。
@property (nonatomic, readonly, copy, nullable) NSString *name; // eg. Apple Inc.
@property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; // street name, eg. Infinite Loop
@property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; // eg. 1
@property (nonatomic, readonly, copy, nullable) NSString *locality; // city, eg. Cupertino
@property (nonatomic, readonly, copy, nullable) NSString *subLocality // neighborhood, common name, eg. Mission District
@property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; // state, eg. CA
@property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // City, eg. Santa Clara
@property (nonatomic, readonly, copy, nullable) NSString *postalCode; // zip code, eg. 95014
@property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; // eg. US
@property (nonatomic, readonly, copy, nullable) NSString *country; // eg. United States
@property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // eg. Lake Tahoe
@property (nonatomic, readonly, copy, nullable) NSString *ocean; // eg. Pacific Ocean
@property (nonatomic, readonly, copy, nullable) NSArray<NSString *> *areasOfInterest;
What ever you want you just add it and get the result.Here is reference link . 什么你想要你只需添加它并得到结果。这里是参考链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.