高德地图的定位和大头针

最近没事研究了下高德地图的使用,简单的介绍下高德地图的定位和大头针功能,至于关键字检索POI等功能 ,可以去看完整的Demo;
使用高德地图 首先导入高德SDK,至于导入的具体方法,可以去看官网:http://lbs.amap.com/api/ios-sdk/guide/create-project/cocoapods;其次是获取Key,参考:http://lbs.amap.com/api/ios-sdk/guide/create-project/get-key
;这些工作完成后。开始写代码
首先在Appdelegate里面注册Key:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //设置高德地图的key
    [AMapServices sharedServices].apiKey =@"写入你申请的key值";
    //开启HTTPS功能
    [[AMapServices sharedServices] setEnableHTTPS:YES];
}

创建BaseMapViewController
BaseMapViewController.h 文件

@interface BaseMapViewController : UIViewController <AMapLocationManagerDelegate,MAMapViewDelegate>

{
    MAUserLocationRepresentation *_Representation;//用户位置显示样式控制
    AMapLocationManager *_locationManager;//
}

@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) MAPointAnnotation *pointAnnotaiton;//创建大头针
- (void)setMapView;

@end

下面是BaseMapViewController.m 文件

#import "BaseMapViewController.h"

@interface BaseMapViewController ()
@end

@implementation BaseMapViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

//    self.navigationController.toolbar.translucent   = YES;
//    self.navigationController.toolbarHidden         = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [_locationManager startUpdatingLocation];
}
- (void)viewDidLoad {
    [super viewDidLoad];

//    //创建地图
    [self setMapView];
}

- (void)setMapView{
    ///地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)
    [AMapServices sharedServices].enableHTTPS = YES;

    _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    _mapView.zoomLevel = 18;//缩放等级
     [self.mapView setDelegate:self];
    _mapView.scaleOrigin = CGPointMake(15, 80);//比例尺位置
    _mapView.showsCompass = YES;//是否显示指南针
    _mapView.compassOrigin = CGPointMake(kScreenWidth - 75, 80);

    self.mapView.mapType = MAMapTypeStandard;//地图的类型
    //如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;

    [self.view addSubview:_mapView];

    //定位管理类
    _locationManager = [[AMapLocationManager alloc] init];
    _locationManager.delegate = self;
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//定位精度
    //   定位超时时间,最低2s,此处设置为10s
    _locationManager.locationTimeout =10;
    //   逆地理请求超时时间,最低2s,此处设置为10s
    _locationManager.reGeocodeTimeout = 10;
    //持续定位
    _locationManager.distanceFilter = 200;//设定定位的最小更新距离
    //    [_locationManager setAllowsBackgroundLocationUpdates:YES];

    //连续定位是否返回逆地理信息
    [_locationManager setLocatingWithReGeocode:YES];
    //如果需要持续定位返回逆地理编码信息 需要做如下设置:
    [_locationManager startUpdatingLocation];

    //用户位置显示样式控制 (这是需要自己定义定位小蓝点时候用的)
       [self setUserLocationRepresentation];
}

自定义小圆点:

#pragma mark - 用户位置显示样式控制
- (void)setUserLocationRepresentation{
    _Representation = [[MAUserLocationRepresentation alloc] init];
    //    _Representation.showsAccuracyRing = NO;//精度圈是否显示,默认YES
        _Representation.showsHeadingIndicator = YES;///是否显示方向指示(MAUserTrackingModeFollowWithHeading模式开启)
    //    _Representation.fillColor = [UIColor redColor];//精度圈填充颜色
    _Representation.strokeColor = [UIColor blueColor];//精度圈 边线颜色
    _Representation.lineWidth = 2;//精度圈边线宽度
    //    _Representation.enablePulseAnnimation = NO;//内部蓝色圆点是否使用律动效果, 默认YES
    //    _Representation.locationDotBgColor = [UIColor greenColor];////定位点背景色,不设置默认白色
    _Representation.locationDotFillColor = [UIColor grayColor];//定位点蓝色圆点颜色,不设置默认蓝色
    _Representation.image = [UIImage imageNamed:@""];//定位图标, 与蓝色原点互斥

    //执行
    [self.mapView updateUserLocationRepresentation:_Representation];

}

下面是Map代理:

#pragma mark - AMapLocationManagerDelegate
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
    //打印经纬度
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    if (reGeocode)
    {
    //打印获取到的位置信息,包括省、市、去等等详细地址
        NSLog(@"reGeocode:%@", reGeocode);

    }

//以下是设置大头针代码
    if (self.pointAnnotaiton == nil)
    {
        self.pointAnnotaiton = [[MAPointAnnotation alloc] init];
        [self.pointAnnotaiton setCoordinate:location.coordinate];

        [self.mapView addAnnotation:self.pointAnnotaiton];
    }
    [self.pointAnnotaiton setTitle:[NSString stringWithFormat:@"%@%@%@",reGeocode.province,reGeocode.city,reGeocode.district]];
    [self.pointAnnotaiton setSubtitle:reGeocode.formattedAddress];
    [self.pointAnnotaiton setCoordinate:location.coordinate];

    [self.mapView setCenterCoordinate:location.coordinate];
}



#pragma mark - MAMapView Delegate

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    //大头针
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";

        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
        }

        annotationView.canShowCallout   = YES;//弹出气泡
        annotationView.animatesDrop     = YES;
        annotationView.draggable        = YES;//可以拖动
        annotationView.image            = [UIImage imageNamed:@"icon_location.png"];

        return annotationView;
    }

    return nil;
}

注:完整demo去GitHub下载,感觉可以 请给个star,嘿嘿嘿:https://github.com/Jadekirin/MWLGaodeMap.git

猜你喜欢

转载自blog.csdn.net/qq_30963589/article/details/72476751