ios的 定位 及高德地图定位
来源:互联网 发布:linux程序 编辑:程序博客网 时间:2024/05/16 08:58
最近休息了一段时间。老有朋友问我定位怎么做的。由于每个需求不同。总结如下。希望用到的朋友能做个参考。
关于定位可分为 普通定位和地图定位。
普通定位只需要显示城市信息或者位置信息。
地图定位需要显示并且标注大头针在地图。
其实原理是一样的。只是展示方式不同而已。
废话不多说了 附上代码
- (id)init
{
self = [super init];
if (self) {
//初始化定位
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 1000.0;
_locationSucsess = NO;
}
return self;
}
其次还需要考虑到版本不同的情况下
ios8的版本需要设置plist文件。
添加字段:
NSLocationAlawaysUsageDescription
NSlocationWhenInUseDescription
对应实现代码:
[locationManager requestAlwaysAuthorization];
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
如果定位失败回掉的方法。
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"locError:%@", error);
_locationSucsess = NO;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
[[NSNotificationCenter defaultCenter]postNotificationName:@"failToLocation" object:nil];
switch([error code]) {
case kCLErrorDenied:
//Access denied by user(发通知给getaround页面)
NSLog(@"Access to Location Services denied by user");
//Do something...
break;
case kCLErrorLocationUnknown:
//Probably temporary...
NSLog( @"Location data unavailable");
//Do something else...
break;
default:
NSLog( @"An unknown error has occurred");
break;
}
}
另外需要实现一个协议内的方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
到现在基本完成一半了。
最重要的需要提一点则是 关于地理编码 和逆向地理编码的。
经纬度信息转为位置信息。
位置信息转为经纬度信息。
这个可以根据具体需要的时候进行使用。
#pragma mark -- 根据地理位置信息获取经纬度 CLGeocoder
-(void)GetLatitudeAndLongitudeBasedOnLocation:(NSString*)loncation{
CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
[myGeocoder geocodeAddressString:loncation completionHandler:^(NSArray *placemarks, NSError *error)
{
if ([placemarks count] > 0 && error == nil) {
NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
NSLog(@"coordAdressLongitude = %f", firstPlacemark.location.coordinate.longitude);
NSLog(@"coordAdressLatitude = %f", firstPlacemark.location.coordinate.latitude);
}
else if ([placemarks count] == 0 && error == nil) {
NSLog(@"Found no placemarks.");
}
else if (error != nil) {
NSLog(@"An error occurred = %@", error); }
}];
}
#pragma mark -- 根据经纬度获取地理位置详细信息 CLGeocoder
-(void)GetmoreInformationInAccordanceWithTheLatitude:(CLLocationDegrees)latitude AndLongitudeLocation:(CLLocationDegrees)longitude{
CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLGeocoder *geocoder=[[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks,
NSError *error)
{
CLPlacemark *placemark=[placemarks objectAtIndex:0];
NSLog(@"地址:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@ \n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n",
placemark.name,
placemark.country,
placemark.postalCode,
placemark.ISOcountryCode,
placemark.ocean,
placemark.inlandWater,
placemark.administrativeArea,
placemark.subAdministrativeArea,
placemark.locality,
placemark.subLocality,
placemark.thoroughfare,
placemark.subThoroughfare);
}];
}
如果打印地理位置信息。则已经完成了定位操作。 其余的则需要根据自己的需求进行 将获取的信息存储并相应赋值即可。
- ios的 定位 及高德地图定位
- 高德地图简单地图及定位
- iOS 高德地图API不能定位及INVALID_USER_SCODE问题
- iOS 高德地图API不能定位及INVALID_USER_SCODE问题
- 高德地图的定位
- ios高德地图定位及定位后数据如何回调
- ios手机定位 -- 高德地图
- IOS 使用高德地图一次定位
- iOS 高德地图 单次定位
- iOS 高德地图 后台定位
- iOS高德地图之定位
- iOS 使用高德地图 MAMapView 无法定位的问题
- 高德地图定位及显示位置
- 高德地图 Android API 的定位原理及使用方法
- 高德地图定位
- 高德地图---定位
- 高德地图定位
- 高德地图定位
- input in python
- say goodbye to “hi,baidu”
- 简述word转换成pdf的过程
- oracle sqlplus登录问题以及odbc连接问题
- 如何快速正确的安装 Ruby, Rails 运行环境
- ios的 定位 及高德地图定位
- HDU 1004 Let the Balloon Rise
- 【转】对前端质量保障的思考 - Barret Lee
- memset pitfalls
- 磁盘长时间插在电脑上,造成文件损坏怎么办?
- C++指针指向堆的创建释放
- HDU 1005 Number Sequence
- 那些求职路上的经验分享与感受
- 母函数