#import "ViewController.h"
//地图
#import <MapKit/MapKit.h>
//定位
#import <CoreLocation/CoreLocation.h>
#import "CustomAnnotationView.h"
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
{
//地图
MKMapView * _mapView;
//定位
CLLocationManager * _locaionManager;
//地理编码
CLGeocoder * _geocoder;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建定位
[self createLocation];
//创建地图
[self createMapView];
//地理编码
[self createGeocoder];
}
#pragma mark - 地理编码
-(void)createGeocoder
{
//实例化
_geocoder = [[CLGeocoder alloc]init];
//正向地理编码,指的就是通过地名获取经纬度
[self getCoordinateByAddress:@"北京"];
//反向地理编码,通过经纬度获取地名
[self getAddressByLongitude:39.896304 latitude:116.410103];
}
-(void)getCoordinateByAddress:(NSString *)adderss
{
[_geocoder geocodeAddressString:adderss completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
{
CLPlacemark * placemark = [placemarks firstObject];
NSLog(@"地名%@~~~区域%@~~~详细信息%@",placemark.name,placemark.region,placemark.addressDictionary);
}];
}
-(void)getAddressByLongitude:(CLLocationDegrees)longtitude latitude:(CLLocationDegrees)latitude
{
//根据经纬度获取位置
CLLocation * location = [[CLLocation alloc]initWithLatitude:latitude longitude:longtitude];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
{
CLPlacemark * placemark = [placemarks firstObject];
NSLog(@"详细地址信息~~~%@",placemark.addressDictionary);
}];
}
#pragma mark - 创建定位
-(void)createLocation
{
//创建定位
_locaionManager = [[CLLocationManager alloc]init];
//判断用户是否开启定位
if (![CLLocationManager locationServicesEnabled])
{
//给用户一个提示,告诉他打开定位服务
NSLog(@"目前尚未开启定位服务,请在设置中打开");
return;
}
//判断用户对当前的应用是否开启了授权
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
//请求用户开启授权
[_locaionManager requestWhenInUseAuthorization];
}
//已经授权,在进行定位的时候开启授权
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)
{
//设置代理
_locaionManager.delegate = self;
//设置精度
_locaionManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置定位间隔,也就是每隔多少米定位一次
_locaionManager.distanceFilter = 10.0;
//开启定位
[_locaionManager startUpdatingLocation];
}
}
#pragma mark - 定位的代理方法
//一旦用户位置发生变化就会频繁的调用这个方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation * location = [locations firstObject];//取出第一个位置
//将位置转化为坐标
CLLocationCoordinate2D coord = location.coordinate;
//完成定位服务之后及时关闭定位
[manager stopUpdatingLocation];
//获取详细信息
NSLog(@"经度%f~~~纬度%f~~~海拔%f~~~航向%f~~~~行走速度%f",coord.longitude,coord.latitude,location.altitude,location.course,location.speed);
}
#pragma mark - 创建地图
-(void)createMapView
{
_mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
//设置代理
_mapView.delegate = self;
//显示用户的位置
_mapView.showsUserLocation = YES;
//显示交通路线
//_mapView.showsTraffic = YES;
//设置需要显示的位置
//设置经纬度
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.896304, 116.410103);
//设置放大的倍数,数值越小,放大的越大
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
[_mapView setRegion:MKCoordinateRegionMake(coord, span)];
//创建大头针
[self addAnnotation:coord];
//给地图添加一个长按手势
UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(createAnnotation:)];
[_mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:_mapView];
}
-(void)createAnnotation:(UILongPressGestureRecognizer *)geature
{
//解决连续创建无数个大头针的问题
if (geature.state != UIGestureRecognizerStateBegan)
{
return;
}
//获取到手势作用的point
CGPoint point = [geature locationInView:_mapView];
//将手势作用的位置转化为坐标
CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:_mapView];
//创建大头针
MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init];
//设置标题
annotation.title = @"你在哪";
//设置副标题
annotation.subtitle = @"我在这";
//设置大头针显示的位置
annotation.coordinate = coord;
[_mapView addAnnotation:annotation];
}
#pragma mark - 创建大头针
-(void)addAnnotation:(CLLocationCoordinate2D)coord
{
MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init];
//设置标题
annotation.title = @"北京";
//设置副标题
annotation.subtitle = @"天安门";
//设置坐标
annotation.coordinate = coord;
//将大头针添加到地图上
[_mapView addAnnotation:annotation];
}
#pragma mark - 地图的代理方法
//设置大头针的属性,改变样式
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//系统自带的大头针
MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
//进行复用
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"];
}
//设置大头针出现的动画
annotationView.animatesDrop = YES;
//设置大头针的颜色,iOS9以前设置大头针颜色的方法.只有三种颜色
annotationView.pinColor = MKPinAnnotationColorPurple;
//iOS9以后用pinTintColor.设置颜色的灵活性更高
//annotationView.pinTintColor = [UIColor yellowColor];
//设置点击的时候出现气泡,默认情况下是不响应交互的,气泡不出现
annotationView.canShowCallout = YES;
//return annotationView;
//自定义的大头针
CustomAnnotationView * customAnnotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"view"];
if (!customAnnotationView) {
customAnnotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"view"];
}
//设置大头针的图片
// customAnnotationView.image = [UIImage imageNamed:@"locationImage"];
// customAnnotationView.canShowCallout = YES;
//添加左视图
UIImageView * leftView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
leftView.image = [UIImage imageNamed:@"4.jpg"];
customAnnotationView.leftCalloutAccessoryView = leftView;
//添加右视图
UIButton * rightView = [UIButton buttonWithType:UIButtonTypeContactAdd];
rightView.frame = CGRectMake(0, 0, 30, 30);
[rightView addTarget:self action:@selector(rightViewClick) forControlEvents:UIControlEventTouchUpInside];
customAnnotationView.rightCalloutAccessoryView = rightView;
//设置动画
[UIView animateWithDuration:2 animations:^{
customAnnotationView.frame = CGRectMake(0, 0, 30, 30);
}];
return customAnnotationView;
}
-(void)rightViewClick
{
NSLog(@"你好");
}
@end
#import <MapKit/MapKit.h>
@interface CustomAnnotationView : MKAnnotationView
@end
#import "CustomAnnotationView.h"
@implementation CustomAnnotationView
//初始化方法
-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
//设置图片
UIImage * image = [UIImage imageNamed:@"7.jpg"];
;
self.image = image;
//设置出现气泡
self.canShowCallout = YES;
}
return self;
}
@end
长按前(默认).png
长按后.png
网友评论