[定位]获取经纬度,街道信息

[objc]
CLLocationManager *locManager=[[CLLocationManager alloc]init]; //创建位置管理器
locManager.desiredAccuracy=kCLLocationAccuracyBest; //指定需要的精度级别
locManager.distanceFilter=100; //设置距离筛选器
[locManager startUpdatingLocation]; //启动更新位置管理

CLLocationDegrees latitude = locManager.location.coordinate.latitude; //float也行,获取当前位置的纬度
CLLocationDegrees longitude = locManager.location.coordinate.longitude; //float也行,获取当前位置的经度
NSLog(@"latitude %f",latitude);
NSLog(@"longitude %f",longitude);

if (longitude == latitude && latitude == 0) {
NSLog(@"定位失败");

}else{

CLGeocodeCompletionHandler handler = ^(NSArray *place, NSError *error) {

for (CLPlacemark *placemark in place) {

NSString *country=[placemark.addressDictionary objectForKey:@"Country"];

NSLog(@"Country %@",country);//国家

NSString *state=[placemark.addressDictionary objectForKey:@"State"];

NSLog(@"State %@",state);//省份

NSString *city=[placemark.addressDictionary objectForKey:@"City"];

NSLog(@"city %@",city);//城市

NSString *street=[placemark.addressDictionary objectForKey:@"Street"];

NSLog(@"Street %@",street);//街道

NSString *addr=[NSString stringWithFormat:@"%@ %@ %@ %@",country,state,city,street];

NSLog(@"addr %@",addr);

break;

}

};

//反向地理编码
CLGeocoder *Geocoder=[[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[Geocoder reverseGeocodeLocation:loc completionHandler:handler];

}
[/objc]

85 thoughts on “[定位]获取经纬度,街道信息

Yupwmh进行回复 取消回复

电子邮件地址不会被公开。