iOS 点/长按地图放/ 移除大头针并显示地址

// 首先建立一个类 接受 <MKAnnotation

LHAnnotation.h

#import <Foundation/Foundation.h>

#import <MapKit/MKMapView.h>

@interface LHAnnotation : NSObject<MKAnnotation>


// Center latitude and longitude of the annotion view.


// The implementation of this property must be KVO compliant.


@property (nonatomic, assign) CLLocationCoordinate2D coordinate;


// Title and subtitle for use by selection UI.


@property (nonatomic, copy) NSString *title;


@property (nonatomic, copy) NSString *subtitle;


@end
// 地图页面

MapViewController.h

#import "ViewController.h"

#import <MapKit/MKMapView.h>



@interface MapViewController : ViewController<CLLocationManagerDelegate>


@property (weak, nonatomic) IBOutlet MKMapView *mapView;


@end

MapViewController.m

#import "MapViewController.h"

#import "LHAnnotation.h"

@interface MapViewController ()<MKMapViewDelegate>


@end


@implementation MapViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

   

//    UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];

    UILongPressGestureRecognizer *mTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];

    

    

    [self.mapView addGestureRecognizer:mTap];

    


    

    

}


- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {

    

    [_mapView removeOverlays:_mapView.overlays];

    [_mapView removeAnnotations:_mapView.annotations];

    

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];//这里touchPoint是点击的某点在地图控件中的位置

    CLLocationCoordinate2D touchMapCoordinate =[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了

    

    NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);

    

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    CLLocation *location = [[CLLocation alloc]initWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];

    __block NSString *name = nil;


        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            for (CLPlacemark *place in placemarks) {

                NSDictionary *location =[place addressDictionary];

                NSLog(@"国家:%@",[location objectForKey:@"Country"]);

                NSLog(@"城市:%@",[location objectForKey:@"State"]);

                NSLog(@"区:%@",[location objectForKey:@"SubLocality"]);

                NSLog(@"位置:%@", place.name);

                NSLog(@"国家:%@", place.country);

                NSLog(@"城市:%@", place.locality);

                NSLog(@"区:%@", place.subLocality);

                NSLog(@"街道:%@", place.thoroughfare);

                NSLog(@"子街道:%@", place.subThoroughfare);

                name = place.name;

                

                LHAnnotation* annotation=[[LHAnnotation alloc]init];

                

                annotation.coordinate=CLLocationCoordinate2DMake(touchMapCoordinate.latitude, touchMapCoordinate.longitude);

                

                

                annotation.title=name;

                

                //    annotation.subtitle=@"123";

                

                [self.mapView addAnnotation:annotation];

                

            }

        }];

    

   

    

    

    

}


-(void)setMap


{

    

    self.mapView=[[MKMapView alloc]init];

    

    self.mapView.frame=self.view.bounds;

    

    /**

     

     *mapType

     

     *

     

     * MKMapTypeStandard = 0,

     

     MKMapTypeSatellite,

     

     MKMapTypeHybrid

     

     */

    

    self.mapView.mapType=MKMapTypeStandard;

    

    /**

     

     *Set to YES to add the user location annotation to the map and start updating its location

     

     */

    

    self.mapView.showsUserLocation=NO;

    

    /**

     

     *Zoom and scroll are enabled by default.

     

     */

    

    self.mapView.scrollEnabled=YES;

    

    self.mapView.zoomEnabled=YES;

    

    /**

     

     *Rotate and pitch are enabled by default on Mac OS X and on iOS 7.0 and later.

     

     */

    

    self.mapView.pitchEnabled=YES;

    

    self.mapView.rotateEnabled=YES;

    

    [self.view addSubview:self.mapView];

    

}
 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值