最近公司需要做范围内打卡的功能,所以研究了下高德地图的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;
}
}];
}
其他还有一些小功能,比如比例缩放,重新定位。离线地图等
其他功能参考一下高德地图,可是实现长按出现选定位置信息,反编译出地理信息,名称等。