iOS - 百度地图最基本操作(定位,手势添加自定义标注)
来源:互联网 发布:js如何实现继承 编辑:程序博客网 时间:2024/05/16 15:29
用 cocoapods 导入百度SDK, 然后开始...
#import "ViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
@interface ViewController ()<BMKMapViewDelegate,CLLocationManagerDelegate> {
BMKMapView *_mapView;
}
@property (nonatomic,strong)CLLocationManager *manager;
@end
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated {
[_mapViewviewWillAppear];
_mapView.delegate =self;
}
- (void)viewWillDisappear:(BOOL)animated {
[_mapViewviewWillDisappear];
_mapView.delegate =nil;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_mapView = [[BMKMapViewalloc] initWithFrame:self.view.bounds];
[self.viewaddSubview:_mapView];
self.manager = [[CLLocationManageralloc] init];
if(![CLLocationManagerlocationServicesEnabled]){
NSLog(@"bu ke yong");
return;
}
NSLog(@"ke yong");
// CLLocationManager这个类是用来定位的
self.manager = [[CLLocationManageralloc]init];
// 间隔多少米去重新定位
self.manager.distanceFilter =10;
// 这个属性是设计定位的精确度
// kCLLocationAccuracyBest这个是最好的精确度
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
// 设置代理
self.manager.delegate =self;
// 如果当前设备大于等于8.0做一下特殊设置
if ([[UIDevicecurrentDevice].systemVersionfloatValue]>=8.0)
{
[self.managerrequestAlwaysAuthorization];
// [manager requestWhenInUseAuthorization];
}
if ([[UIDevicecurrentDevice].systemVersionfloatValue]>=9.0)
{
[self.managerallowsBackgroundLocationUpdates];//允许后台定位更新
}
// 开始定位
[self.managerstartUpdatingLocation];
// 手势操作
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressOption:)];
gesture.minimumPressDuration =0.5; // 默认就是 0.5s
[_mapViewaddGestureRecognizer:gesture];
}
//这个代理方法是在定位失败的时候会调用
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnullNSError *)error{
NSLog(@"==error===>>>%@",error.localizedDescription);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnullNSArray<CLLocation *> *)locations{
CLLocation *location = [locationslastObject];
CLGeocoder *geocoder = [[CLGeocoderalloc]init];
[geocoder reverseGeocodeLocation:locationcompletionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
// CLPlacemark这个类里面装的是你当前定位到的那个地点的信息
CLPlacemark *mark = [placemarksfirstObject];
// mark.country
// mark.location.coordinate
//NSLog(@"======>>%@------%@=====>>>%@",mark.locality,mark.name,mark.addressDictionary);
// 添加一个PointAnnotation
BMKPointAnnotation *annotation = [[BMKPointAnnotationalloc]init];
CLLocationCoordinate2D coor = mark.location.coordinate;
annotation.coordinate = coor;
annotation.subtitle = mark.name;
annotation.title = mark.locality;
[_mapViewaddAnnotation:annotation];
_mapView.centerCoordinate = coor;
}];
}
// 重写方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotationisKindOfClass:[BMKPointAnnotationclass]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor =BMKPinAnnotationColorRed;
newAnnotationView.animatesDrop =YES;//设置该标注点动画显示
// 设置左视图
UIImageView *leftImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0, 40,40)];
leftImageView.image = [UIImageimageNamed:@"0.png"];
newAnnotationView.leftCalloutAccessoryView = leftImageView;
// 设置右视图
UIButton *rightButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
rightButton.frame =CGRectMake(0,0, 200,40);
[rightButton setTitle:@"H"forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[rightButton addTarget:selfaction:@selector(sendClick)forControlEvents:UIControlEventTouchUpInside];
newAnnotationView.rightCalloutAccessoryView = rightButton;
return newAnnotationView;
}
returnnil;
}
#pragma mark - 为地图标注点添加长按手势
- (void)longPressOption:(UILongPressGestureRecognizer *)gesture {
if (gesture.state ==UIGestureRecognizerStateRecognized) {
CGPoint point = [gesturelocationInView:_mapView];
CLLocationCoordinate2D coord = [_mapViewconvertPoint:point toCoordinateFromView:_mapView];
CLGeocoder *geocoder = [[CLGeocoderalloc]init];
CLLocation *location = [[CLLocationalloc]initWithLatitude:coord.latitudelongitude:coord.longitude];
[geocoder reverseGeocodeLocation:locationcompletionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *mark = [placemarkslastObject];
BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotationalloc] init];
pointAnnotation.title = mark.locality;
pointAnnotation.subtitle = mark.name;
pointAnnotation.coordinate = coord;
[_mapViewaddAnnotation:pointAnnotation];
}];
}
}
#pragma mark - 标注按钮点击事件
- (void)sendClick {
NSLog(@"这里是点击所有标注都会调用的方法,,,");
}
- iOS - 百度地图最基本操作(定位,手势添加自定义标注)
- iOS-百度地图添加标注自定义气泡
- 百度地图自定义标注物+自动定位
- 百度地图上自定义添加一个标注
- iOS 自定义百度地图标注气泡
- iOS中设置百度地图自定义标注图片,自定义泡泡
- 百度地图自定义大头针图片和添加标注
- ios 地图 自定义标注
- 百度地图添加标注点
- iOS Swift百度地图 添加多个标注
- 百度地图自定义标注标记
- 关于iOS百度地图标注
- 百度地图API,自定义地图标注
- 百度地图API,自定义地图标注
- 百度地图定位操作
- iOS百度地图定位
- 百度地图之地址解析和浏览器定位以及添加可拖拽标注
- 百度地图API3.1.1版之添加标注物+UI控制+定位
- python根据类名映射
- java.util.vector中的vector的详细用法
- BeanFactoryPostProcessor和BeanPostProcessor的区别
- uboot的常用命令
- 六位设计师分享的数位板使用经历
- iOS - 百度地图最基本操作(定位,手势添加自定义标注)
- DOM 解析 XML
- ThinkPad E550禁用触摸板
- Spring InitializingBean和init-method
- 第十六周项目—点结构体2
- uboot常用的环境变量
- UIGrid实现卡牌曲线层叠效果
- 【Android】IntentService & HandlerThread源码解析
- javascript学习——constructor、prototype、_proto_的区别