gpt4 book ai didi

ios - 在 iOS 中使用 Geocoder 类根据地址获取经纬度

转载 作者:技术小花猫 更新时间:2023-10-29 10:41:14 27 4
gpt4 key购买 nike

我根据经度和纬度值获得了当前位置,然后我还使用注释在谷歌地图上获得了多个位置。现在我想根据地址(即街道、城市和县)获取经度和纬度值。需要一些关于如何实现这一点的指导。

到目前为止,这是我尝试过的:-

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize streetField = _streetField, cityField = _cityField, countryField = _countryField, fetchCoordinatesButton = _fetchCoordinatesButton, nameLabel = _nameLabel, coordinatesLabel = _coordinatesLabel;
@synthesize geocoder = _geocoder;


- (void)viewDidLoad
{

[super viewDidLoad];
_streetField.delegate=self;
_cityField.delegate=self;
_countryField.delegate=self;

// Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)fetchCoordinates:(id)sender {
NSLog(@"Fetch Coordinates");
if (!self.geocoder) {
NSLog(@"Geocdoing");
self.geocoder = [[CLGeocoder alloc] init];
}

NSString *address = [NSString stringWithFormat:@"%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];
NSLog(@"GET Addres%@",address);

self.fetchCoordinatesButton.enabled = NO;

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Fetch Gecodingaddress");
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];

NSLog(@"GET placemark%@",placemark);

CLLocation *location = placemark.location;

NSLog(@"GET location%@",location);

CLLocationCoordinate2D coordinate = location.coordinate;


self.coordinatesLabel.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude];

NSLog(@"CoordinatesLabel%@",self.coordinatesLabel.text);


if ([placemark.areasOfInterest count] > 0) {
NSString *areaOfInterest = [placemark.areasOfInterest objectAtIndex:0];
self.nameLabel.text = areaOfInterest;
NSLog(@"NameLabe%@",self.nameLabel.text);
}
}

self.fetchCoordinatesButton.enabled = YES;
}];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end

以上代码无法为我提供经纬度。需要一些帮助来了解我在这里做错了什么,或者我是否遗漏了什么。

提前致谢。

最佳答案

这是一个非常古老的答案,请检查新的更新

编辑:

在使用此检查 iOS8 更新之前

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

这是为了获取基于纬度和经度的用户区域,例如街道名称、州名称、国家/地区。

-(CLLocationCoordinate2D) getLocationFromAddressString: (NSString*) addressStr {
double latitude = 0, longitude = 0;
NSString *esc_addr = [addressStr 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];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude = longitude;
NSLog(@"View Controller get Location Logitute : %f",center.latitude);
NSLog(@"View Controller get Location Latitute : %f",center.longitude);
return center;

}

在viewdidload方法中或者根据你的项目在某处调用这样的方法

[self getLocationFromAddressString:@"chennai"];

只需在您的浏览器中传递它 http://maps.google.com/maps/api/geocode/json?sensor=false&address=chennai

你将得到带有纬度和经度的 json 格式

http://maps.google.com/maps/api/geocode/json?sensor=false&address=@"your city name here"




NSString *address = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];

这个方法的用法....

CLLocationCoordinate2D center;
center=[self getLocationFromAddressString:@"uthangarai"];
double latFrom=&center.latitude;
double lonFrom=&center.longitude;

NSLog(@"View Controller get Location Logitute : %f",latFrom);
NSLog(@"View Controller get Location Latitute : %f",lonFrom);

关于ios - 在 iOS 中使用 Geocoder 类根据地址获取经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503375/

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