#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MyAnnotationView.h"
#import "MyAnnotation.h"
@interface ViewController ()<MKMapViewDelegate>
{
CLLocationManager *manager;
MKMapView *mapViews;
}
@end
/**
*
1.使用地图需要导入MapKit框架,也需要用户授权。
2.CoreLocation 是数据类,定位信息,地理编码,反地理编码;mapKit是控件类,显示在屏幕上的视图
3.地图视图 MKMapView 大头针视图 MKPinAnnotationView
*/
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"地图没打开");}
//不管是定位还是使用地图,只要用到定位服务都需要获取用户授权
manager = [[CLLocationManager alloc]init];
[manager requestAlwaysAuthorization];
mapViews = [[MKMapView alloc]initWithFrame:self.view.frame];
mapViews.delegate = self;
[self.view addSubview:mapViews];
mapViews.mapType = MKMapTypeStandard;
//设置用户是否允许旋转
mapViews.rotateEnabled = NO;
//是否允许放大缩小地图
mapViews.zoomEnabled = YES;
//是否允许滚动地图
mapViews.scrollEnabled = YES;
//设置地图是否有3D效果
mapViews.pitchEnabled = YES;
//设置是否显示用户的位置
mapViews.showsUserLocation = YES;
//设置是否显示附近的建筑物(MKMapTypeHybrid,MKMapTypeStandard有效)
mapViews.showsBuildings = YES;
//设置是否显示兴趣点(MKMapTypeHybrid,MKMapTypeStandard有效)
mapViews.showsPointsOfInterest = YES;
UILongPressGestureRecognizer *addPoint = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addPointView:)];
[mapViews addGestureRecognizer:addPoint];
}
- (void)addPointView:(UILongPressGestureRecognizer *)sender
{
//解决长按手势触发两次问题,在手势开始的时候执行里面的方法
if (sender.state == UIGestureRecognizerStateBegan) {
//把视图上的point转换成地图上面的坐标
//- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view
CGPoint point = [sender locationInView:mapViews];
NSLog(@"%f %f",point.x,point.y);
CLLocationCoordinate2D coordinate = [mapViews convertPoint:point toCoordinateFromView:mapViews];
NSLog(@"纬度:%f 经度:%f",coordinate.latitude,coordinate.longitude);
MyAnnotation *pointannitation = [[MyAnnotation alloc]init];
pointannitation.coordinate = coordinate;
//通过视图上的点获得经纬度;再把经纬度转换成CLLocation
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
CLLocation *locations = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:locations completionHandler:^(NSArray *placemarks, NSError *error) {
//异步操作 将数据写到里面
CLPlacemark *placeMark = [placemarks lastObject];
pointannitation.title = placeMark.locality;
pointannitation.title = placeMark.name;
[mapViews addAnnotation:pointannitation];
}];
}
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"加载错误");
}
//完成更新定位
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//变化比例
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
//变化区域
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
//动画效果
[mapViews setRegion:region animated:YES];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
[annotation setCoordinate:userLocation.location.coordinate];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMarks = [placemarks lastObject];
//用户的位置
userLocation.title = placeMarks.name;
userLocation.subtitle = placeMarks.locality;
}];
}
//选择的是哪个大头针
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[MKPointAnnotation class]]) {
view.image = [UIImage imageNamed:@"{)}4%0JTT7}4720(`AFJ1HD"];
}
}
//取消选择的是哪个大头针
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[MKPointAnnotation class]]) {
view.image = [UIImage imageNamed:@"AVG%}9C@M3QL]O$3SSBK0$9"];
}
}
//返回大头针视图(类似表示图定制cell的方法)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MyAnnotation class]] ) {
NSString *annotationID = @"ID";
MyAnnotationView *annotationView = (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID];
if (!annotationView) {
annotationView = [[MyAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
}
return annotationView;
}
return nil;
}
@end