gpt4 book ai didi

ios - 如何在 IOS 中使用经纬度通过 CLlocationManager 获取城市名称?

转载 作者:可可西里 更新时间:2023-11-01 03:49:09 32 4
gpt4 key购买 nike

我正在尝试使用纬度和经度通过 CLLocationManager 获取地址,但它只返回州和国家名称,我也想要城市,我附上了我的代码谁能告诉我如何更改我的代码以获取城市名称.下面是我的代码

- (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);
}

}];
}

最佳答案

试试这个..

-(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;
}

发送逗号分隔的纬度经度..并尝试它工作正常

引用链接 Get Address From Latitude Longitude using Apple Function in iOS & iPad

关于ios - 如何在 IOS 中使用经纬度通过 CLlocationManager 获取城市名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24888536/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com