自定义大头针


#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;

    


    //设置是否显示附近的建筑物(MKMapTypeHybridMKMapTypeStandard有效)

    mapViews.showsBuildings = YES;

    

    //设置是否显示兴趣点(MKMapTypeHybridMKMapTypeStandard有效)

    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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值