使用iOS中的Geocoder类,根据地址获取纬度和经度 [英] Get latitude and longitude based on Address using Geocoder class in iOS
问题描述
我根据经度和纬度值获得了当前位置,然后我还使用Annotation在Google地图上获得了多个位置。现在我想根据地址(即街道,城市和县)得到经度和纬度值。需要一些指导如何实现。
I got the current location based on the Longitude and Latitude values and then I also got multiple places on google map using Annotation. Now I want to get the longitude and latitude values based on the Address( i.e street,city and county).Need some guidance on how this can be achieved.
到目前为止,这就是我的尝试: -
Till now, this is what I have tried:-
#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
以上代码无法给我纬度和经度。需要一些帮助,我在这里做错了什么,或者我错过了什么。
Above code is not working to give me the latitude and longitude.Need some help on what am I doing wrong here or if I am missing on something.
先谢谢。
推荐答案
编辑:
之前在iOS8更新时使用此检查
Before using this check with iOS8 updation
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
这是为了获得基于纬度和长期的用户区域,如街道名称,州名,国家。
This is for getting lat and long based user area like street name,state name,country.
-(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方法或某处根据以下方法调用此方法你的项目
call the method like this in viewdidload method or somewhere according to your project
[self getLocationFromAddressString:@"chennai"];
只需在浏览器中传递
http://maps.google.com/maps/api/geocode/json?sensor=false&address=chennai
你将拥有lat和lon的json格式
and you will have json format with lat and lon
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];
此方法的使用....
the usage of this method....
CLLocationCoordinate2D center;
center=[self getLocationFromAddressString:@"uthangarai"];
double latFrom=¢er.latitude;
double lonFrom=¢er.longitude;
NSLog(@"View Controller get Location Logitute : %f",latFrom);
NSLog(@"View Controller get Location Latitute : %f",lonFrom);
这篇关于使用iOS中的Geocoder类,根据地址获取纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!