IOS 自带地图开发

1、导入库
CoreLocation、 MapKit
2、在AppDelegate开启定位功能
- (void)startLocation
{
   
//1.定位管理器
   
_locationManager = [[CLLocationManager alloc] init];
   
if (![CLLocationManager locationServicesEnabled]) {
       
MyLog(@"定位服务当前可能尚未打开,请设置打开!");
       
return;
    }
   
//2.如果没有授权则请求用户授权
   
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [
_locationManager requestWhenInUseAuthorization];
    }
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
       
//2.1设置代理
       
_locationManager.delegate = self;
       
//2.2设置定位精度
       
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
       
//2.3设置定位频率
       
_locationManager.distanceFilter = 100.0;
       
//2.4开启定位功能
        [
_locationManager startUpdatingLocation];
    }
}

#pragma mark CoreLocation代理
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
   
CLLocation *location = [locations firstObject];
   
CLLocationCoordinate2D coordinate = location.coordinate;
    MyLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
}

3、坐标转地址
- (void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude
{
   
//反地理编码
   
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
   
CLGeocoder *geoCode = [[CLGeocoder alloc] init];
    [geoCode
reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
CLPlacemark *placeMark = [placemarks firstObject];
       
NSDictionary *addressDict = placeMark.addressDictionary;
       
NSString *name = [addressDict objectForKey:@"Name"];
       
MyLog(@"详细信息:%@",name);
       
_addressTextField.text = name;
    }];
}

4、调用地图功能
#pragma mark 添加地图控件
- (void)addMapKit
{
   
//1.初始化及设置代理
   
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
   
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
   
_mapView.showsUserLocation = YES;
   
_mapView.delegate = self;
    [
self.view addSubview:_mapView];
   
   
//2.请求定位服务
   
_locationManager = [[CLLocationManager alloc] init];
   
if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
        [
_locationManager requestWhenInUseAuthorization];
    }
   
   
//3.用户位置跟踪
   
_mapView.userTrackingMode = MKUserTrackingModeFollow;
   
   
//4.设置地图类型
   
_mapView.mapType = MKMapTypeStandard;
   
   
//5.添加大头针
    [self addAnnotation];
}

#pragma mark 添加大头针
- (void)addAnnotation
{
   
CLLocationCoordinate2D shopCoordinate = CLLocationCoordinate2DMake(_latitude, _longitude);
   
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.
coordinate = shopCoordinate;
    annotation.
title = _name;
    [
_mapView addAnnotation:annotation];
}

#pragma mark - MapView代理
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
   
if (_mapView) return;
   
   
//1.位置
   
CLLocationCoordinate2D center = userLocation.location.coordinate;
   
   
//2.跨度
   
MKCoordinateRegion region = MKCoordinateRegionMake(center, kSpan);
   
   
//3.区域
    [mapView
setRegion:region animated:YES];
   
   
_mapView = mapView;
}

#pragma mark 自定义大头针图片
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
   
if ([annotation isKindOfClass:[MKPointAnnotation class]]){
       
MKPinAnnotationView *newAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.
pinColor = MKPinAnnotationColorPurple;
       
// 设置该标注点动画显示
        newAnnotationView.
animatesDrop = YES;
        newAnnotationView.
annotation = annotation;
       
//把大头针换成别的图片
        newAnnotationView.
image = [UIImage imageNamed:@"my_mapLocate_annotation.png"];
       
return newAnnotationView;
    }
   
return nil;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值