在iOS开发中要用到地图时选择很多,例如第三方的高德地图、百度地图都是很好的选择,当然最基础的则是iOS中自带的地图,了解基础有助于我们更好的使用地图。
1.所用的框架:
定位:CoreLocation框架
地图:MapKit框架
2.用户配置
1.导入框架MapKit.framework
2.进行请求授权,在info.plist文件中进行配置key。注意:在这里有两种授权
1)NSLocationAlwaysUsageDescription:用户同意授权后可永久使用
2)NSLocationWhenInUseUsageDescription: 当用户打开对应的应用时可以使用(推荐使用)
3.定位与地图的使用
CoreLocation框架与MapKit框架是两个独立的单元,可单独使用,CoreLocation负责定位,MapKit负责地图的开发,通常会配合使用。
1.CoreLocation
这里需要用到操作类CLLocationManager
1)初始化操作类:
@property (nonatomic,strong) CLLocationManager *locationM;
这里我们使用一个懒加载,对操作类进行一次授权赋值
if (!_locationM) {
self.locationM = [[CLLocationManager alloc] init];
//判断iOS版本
if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
//此处与info.plist授权配置一致
[_locationM requestWhenInUseAuthorization];
}
}
return _locationM;
2)开始定位
[self.locationM startUpdatingLocation];
3)实现协议方法
定位成功后方法
注意:此方法在定位成功后会多次重复调用
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
//获取当前位置
//注意:此处获取的经纬度为WGS84系经纬度,在与MapKit配合使用时,需转为GCJ-02经纬度
CLLocation *currentLocation = [locations lastObject];
//地理编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placemark = placemarks[0];
//所在城市
NSString *city = placemark.locality;
//社区
NSString *neighborhood = placemark.subLocality;
//街道
NSString *thoroughfare = placemark.thoroughfare;
NSString *subThoroughfare = placemark.subThoroughfare;
//行政区域:省份
NSString *province = placemark.administrativeArea;
//子行政区域
NSString *subProvince = placemark.subAdministrativeArea;
//四大直辖市的城市信息无法通过locality获取,通过省份获取
}
}];
}
4)结束定位
[manager stopUpdatingLocation];
2.MapKit
1)地图设置
地图设置为属性
代理协议:MKMapViewDelegate
@property (nonatomic,strong) MKMapView *mapView;
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 150)];
//地图类型,枚举值
self.mapView.mapType = 0;
self.mapView.showsBuildings = YES;
//显示用户当前位置
self.mapView.showsUserLocation = YES;
//罗盘与比例尺需在iOS9.0及以后版本中方可使用
self.mapView.showsCompass = YES;
self.mapView.showsScale = YES;
//设置代理
self.mapView.delegate = self;
定位成功后代理方法
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
注意:此处定位成功后,代理方法中返回的userLocation
经纬度为GCJ-02坐标,也就是‘火星坐标’,在mapView地图中,地图也已做了对应的偏移,故坐标不需要转换,可直接在地图上使用。
2)地图显示区域
所用结构体:MKCoordinateSpan
、MKCoordinateRegion
//显示区域跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
//显示区域,第一个参数:经纬度为中心,第二个参数:跨区域显示
MKCoordinateRegion region = MKCoordinateRegionMake(BGCJlocation, span);
//在地图上显示
[self.mapView setRegion:region animated:YES];
3)大头针
所用类:MapAnnotation
//初始化
_mkAnnotation = [[MapAnnotation alloc] init];
//大头针所在经纬度
_mkAnnotation.coordinate = BGCJlocation;
//大头针title
_mkAnnotation.title = @"目的地";
//子标题
_mkAnnotation.subtitle = @"xxx";
//添加到地图上
[self.mapView addAnnotation:_mkAnnotation];
此大头针模型可与显示区域配合使用
4.WGS84经纬度转GCJ-02经纬度
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
const double pi = 3.14159265358979324;
@implementation WGS84TOGCJ02
//返回值为GCJ02经纬度
+ (CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
{
CLLocationCoordinate2D adjustLoc;
if([self isLocationOutOfChina:wgsLoc]){
adjustLoc = wgsLoc;
}else{
NSLog(@"转换");
double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
double radLat = wgsLoc.latitude / 180.0 * pi;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
adjustLoc.latitude = wgsLoc.latitude + adjustLat;
adjustLoc.longitude = wgsLoc.longitude + adjustLon;
}
return adjustLoc;
}
//判断是不是在中国
+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location
{
if (location.longitude < 72.004 && location.longitude > 137.8347 && location.latitude < 0.8293 && location.latitude > 55.8271){
return YES;
}
return NO;
}
+(double)transformLatWithX:(double)x withY:(double)y
{
double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));
lat += (20.0 * sin(6.0 * x * pi) + 20.0 *sin(2.0 * x * pi)) * 2.0 / 3.0;
lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
return lat;
}
+(double)transformLonWithX:(double)x withY:(double)y
{
double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
return lon;
}
网友评论