自己尝试等装一下牛叉的demoiOS Developer

IOS地图中的大头针的基本使用

2016-08-18  本文已影响181人  梦醒繁华
6.gif 2.png
基本使用
3.png
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface LCAnno : NSObject<MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) NSString *subtitle;
@end
- (void)addAnnoWithPT:(CLLocationCoordinate2D)pt{
    __block LCAnno *anno = [[LCAnno alloc]init];
    anno.coordinate = pt;
    anno.title = @"大神";
    anno.subtitle = @"在这里";
    
    [self.mapView addAnnotation:anno];
    
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:anno.coordinate.latitude longitude:anno.coordinate.longitude];
    
    [self.geoC reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *pl = [placemarks firstObject];
        anno.title = pl.locality;
        anno.subtitle = pl.thoroughfare;
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    //1.获取当前触摸点
    CGPoint point = [[touches anyObject] locationInView:self.mapView];
    
    //2.转换成经纬度
    CLLocationCoordinate2D pt = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    
    //3.添加大头针
    [self addAnnoWithPT:pt];
    
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //移除大头针(模型)
    NSArray *annos = self.mapView.annotations;
    
    [self.mapView removeAnnotations:annos];
}
上一篇 下一篇

猜你喜欢

热点阅读