AMap高德地图自定义指针,定位mapview新方法!


最近公司需要做范围内打卡的功能,所以研究了下高德地图的SDK,首先注册申请key就不多讲了,百度有很详细的资料,下面主要记录下实现方法。demo地址在最下方:下载后自行更新cocoapod文件下载依赖即可运行

包含功能1、定位 2、逆地理位置3、设置范围打卡 4、自定义指针选定位置

我用的是pod管理工程,这是所有的sdk文件

pod 'AFNetworking'

pod 'FMDB'

pod 'YYKit'


pod 'AMap3DMap'

pod 'AMapSearch'


更新下载好cocoapod依赖之后在appdelegate里面设置关键key

  // 配置高德地图

    [AMapServicessharedServices].apiKey =@"*********************";

    

这样我们就可以正常的写我们的方法了,写一个界面,里面显示我们的地图,定位信息

在这里我们用的是MAMapView,而不是用的manager网上有很多方法都是用的manager,我感觉既然有地图试图为什么不直接用呢?


// 设置打卡地点经纬度

@property (nonatomic,strong)NSString *localLongitude;

@property (nonatomic,strong)NSString *localLatitude;


// 高德地图

@property (nonatomic,strong)MAMapView *mapView;

//全局的大头针

@property(nonatomic,strong)MAPointAnnotation *pointAnnotation;

// 定位当前位置的经纬度

@property (nonatomic,strong)NSString *userLongitude;

@property (nonatomic,strong)NSString *userLatitude;


在viewdidappear里面家在信息

    // 进入界面就以定位点为地图中心

    [self.mapViewsetCenterCoordinate:CLLocationCoordinate2DMake([self.userLatitudefloatValue], [self.userLongitudefloatValue])animated:NO];

    // 将绘制的图形添加到地图上

    [self.mapViewaddOverlays:self.circles];


在viewdidload加载地图信息


    // https配置

    [AMapServicessharedServices].enableHTTPS =YES;

    // 初始化地图

    self.mapView = [[MAMapViewalloc]initWithFrame:CGRectMake(0,NAVIGATIONBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT -NAVIGATIONBAR_HEIGHT)];

    self.mapView.delegate =self;

    // 显示定位小蓝点

    self.mapView.showsUserLocation =YES;

    self.mapView.showsCompass =NO;

    //创建手势对象

    UILongPressGestureRecognizer *tap =[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];

    [_mapViewaddGestureRecognizer:tap];

    

    //全局的大头针

    _pointAnnotation = [[MAPointAnnotationalloc]init];

    _pointAnnotation.coordinate =CLLocationCoordinate2DMake([self.userLatitudedoubleValue], [self.userLongitudedoubleValue]);

    [_mapViewaddAnnotation:_pointAnnotation];

    [_mapViewsetCenterCoordinate:CLLocationCoordinate2DMake([self.userLatitudedoubleValue], [self.userLongitudedoubleValue])animated:YES];    

    // 追踪用户的location更新

    self.mapView.userTrackingMode =MAUserTrackingModeFollow;

    // 放大等级

    [self.mapViewsetZoomLevel:16animated:YES];

    

    [self.viewaddSubview:self.mapView];

下面是设置代理方法,动态打开需要选中一个点作为打开地点,我们添加一个长按手势获取动态位置,然后作为打卡点,通过获得的经纬度逆地理位置信息显示在title上,然后与定位获得的经纬度比较判定是否满足打卡范围。这样就完整实现了定位打卡的功能。

下面是长按手势方法

//自定义大头针我这里只是把大头针变成一张自定义的图片

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation

{

    if ([annotationisKindOfClass:[MAPointAnnotationclass]])

    {

        staticNSString *reuseIndetifier =@"annotationReuseIndetifier";

        MAAnnotationView *annotationView = (MAAnnotationView *)[mapViewdequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

        if (annotationView ==nil)

        {

            annotationView = [[MAAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:reuseIndetifier];

        }

        annotationView.image = [UIImageimageNamed:@"locationPoint_select"];

        //设置中心点偏移,使得标注底部中间点成为经纬度对应点

        annotationView.centerOffset =CGPointMake(0, -18);

        return annotationView;

    }

    returnnil;

}


- (void)longPress:(UIGestureRecognizer*)gestureRecognizer{

//   if (gestureRecognizer.state == UIGestureRecognizerStateEnded){

//       return;

//   }

   [_mapViewremoveAnnotation:_pointAnnotation];

    //坐标转换

    CGPoint touchPoint = [gestureRecognizer locationInView:_mapView];

    CLLocationCoordinate2D touchMapCoordinate =

   [_mapViewconvertPoint:touchPointtoCoordinateFromView:_mapView];

    

    _pointAnnotation.coordinate = touchMapCoordinate;

    //_pointAnnotation.title = @"设置名字";

    

   [_mapViewaddAnnotation:_pointAnnotation];

    

   [selfsetLocationWithLatitude:touchMapCoordinate.latitudeAndLongitude:touchMapCoordinate.longitude];

}

- (void)setLocationWithLatitude:(CLLocationDegrees)latitude AndLongitude:(CLLocationDegrees)longitude{

    

    NSString *latitudeStr = [NSStringstringWithFormat:@"%f",latitude];

    NSString *longitudeStr = [NSStringstringWithFormat:@"%f",longitude];

    

    NSLog(@"自己加的%@%@",latitudeStr,longitudeStr);

    //反编码经纬度---->位置信息

    CLLocation *location=[[CLLocationalloc]initWithLatitude:latitudelongitude:longitude];

    CLGeocoder *geocoder=[[CLGeocoderalloc]init];

    [geocoder reverseGeocodeLocation:locationcompletionHandler:^(NSArray *placemarks,NSError *error) {

        if (error) {

            NSLog(@"反编码失败:%@",error);

              [selfpresentAlertControllerWithTitle:@"提示"message:[NSStringstringWithFormat:@"该网点经纬度信息有误,请重新标注"]cancelTitle:@"好的"];

        }else{

            

            self.localLatitude = latitudeStr;

            self.localLongitude = longitudeStr;

            

            // 重新绘制打卡范围

            [selfsetAddress];

            

            //NSLog(@"反编码成功:%@",placemarks);

            CLPlacemark *placemark=[placemarkslastObject];

            //NSLog(@"%@",placemark.addressDictionary[@"FormattedAddressLines"]);

            NSDictionary *addressDic=placemark.addressDictionary;

            

            NSString *state=[addressDicobjectForKey:@"State"];

            NSString *city=[addressDicobjectForKey:@"City"];

            NSString *subLocality=[addressDicobjectForKey:@"SubLocality"];

            NSString *street=[addressDicobjectForKey:@"Street"];

            NSString *Thoroughfare=[addressDicobjectForKey:@"Thoroughfare"];

            

            NSString *strLocation;

            if (street.length ==0 || street ==NULL || [streetisEqualToString:@"(null)"]) {

                strLocation= [NSStringstringWithFormat:@"%@",subLocality];

            }elseif (Thoroughfare.length ==0 || Thoroughfare == NULL || [ThoroughfareisEqualToString:@"(null)"]||([streetisEqualToString:Thoroughfare])){

                strLocation= [NSStringstringWithFormat:@"%@%@",subLocality,street];

                

            }else{

                strLocation= [NSStringstringWithFormat:@"%@%@%@",subLocality,street,Thoroughfare];

            }

            NSLog(@"%@%@%@",state,city,strLocation);

            self.title = strLocation;

            

        }

    }];

}

其他还有一些小功能,比如比例缩放,重新定位。离线地图等

其他功能参考一下高德地图,可是实现长按出现选定位置信息,反编译出地理信息,名称等。

demo地址(这招我跟别人学的,哈哈哈),关注公众号,行走的java,回复001获得----多谢支持,共同学习一起进步,我不是为了流量主哦,里面有很多干货,以后会更多!保你关注不亏!





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值