定位 - MapKit-自定义大头针
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
?
@interface PPAnnotation : NSObject<MKAnnotation>
?
/**
?*? 大頭針的位置
?*/
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/**
?*? 大頭針標題
?*/
@property (nonatomic, copy) NSString *title;
/**
?*? 大頭針的子標題
?*/
@property (nonatomic, copy) NSString *subtitle;
?
/**
?*? 圖標
?*/
@property (nonatomic, copy) NSString *icon;
?
@end
?------------------------------------------------------------------------------------------------------------------
#import <MapKit/MapKit.h>
?
@interface PPAnnotationView : MKAnnotationView
?
/**
?*? 快速創建 大頭針
?*
?*? @param mapView mapView
?*/
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView;
?
@end
#import "PPAnnotationView.h"
#import "PPAnnotation.h"
?
@implementation PPAnnotationView
?
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
? ? if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
? ? ? ? // 初始化
?? ? ? ?
?? ? ?
? ? ? ? // 設置顯示標題
? ? ? ? self.canShowCallout = YES;
?? ? ? ?
? ? ? ? // 設置輔助視圖
? ? ? ? self.leftCalloutAccessoryView = [[UISwitch alloc] init];
? ? ? ? self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
? ? }
? ? return self;
}
?
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView
{
? ? static NSString *ID = @"anno";
? ? // 1. 從緩存中取
? ? // [注意] 默認情況下, MKAnnotationView 是無法顯示的, 如果想自定義大頭針, 需要使用MKAnnotationView的子類 MKPinAnnotationView
? ? // [注意] 如果是自定義的大頭針, 默認情況點擊大頭針不會顯示標題和副標題, 需要手動設置顯示
? ? //? ? MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
? ? PPAnnotationView *annoView = (PPAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
?? ?
? ? // 2. 如果緩存中沒有, 創建一個新的大頭針
? ? if (annoView == nil) {
? ? ? ? annoView = [[PPAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
? ? }
? ? return annoView;
}
?
//- (void)setAnnotation:(id<MKAnnotation>)annotation
- (void)setAnnotation:(PPAnnotation *)annotation
{
? ? [super setAnnotation:annotation];
?? ?
? ? // 處理自己特有的操作
? ? self.image = [UIImage imageNamed:annotation.icon];
}
?
@end
?---------------------------------------------------------------------------------------------
#import "ViewController.h"
//#import <MapKit/MapKit.h>
#import "PPAnnotation.h"
?
#import "PPAnnotationView.h"
?
@interface ViewController ()<MKMapViewDelegate>
?
/**
?*? 地圖
?*/
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
?
?
@property (nonatomic, strong) CLLocationManager *mgr;
?
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
?
@implementation ViewController
/**
?*? 添加大頭針
?*/
- (IBAction)addAnnotation:(id)sender {
? ? PPAnnotation * annotation = [[PPAnnotation alloc] init];
? ? annotation.title = @"北京智德創輝網絡科技有限公司";
? ? annotation.subtitle = @"RO";
? ? annotation.icon = @"category_4";
?? ?
? ? CGFloat latitude = 39.915094 + arc4random_uniform(20);
? ? CGFloat longitude = 116.487775 + arc4random_uniform(20);
? ? annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
?? ?
? ? [self.mapView addAnnotation:annotation];
?? ?
}
?
- (CLLocationManager *)mgr{
? ? if (!_mgr) {
? ? ? ? _mgr = [[CLLocationManager alloc] init];
? ? }
? ? return _mgr;
}
?
?
- (CLGeocoder *)geocoder{
? ? if (!_geocoder) {
? ? ? ? _geocoder = [[CLGeocoder alloc] init];
? ? }
? ? return _geocoder;
}
?
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // ios 8
? ? if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
? ? ? ? [self.mgr requestAlwaysAuthorization];
? ? }
?? ?
? ? // 設置代理
? ? self.mapView.delegate = self;
?? ?
? ? // 設置模式
? ? self.mapView.mapType = MKMapTypeStandard;
?? ?
? ? // 設置跟蹤
? ? self.mapView.userTrackingMode = MKUserTrackingModeFollow;
?? ?
? ? // 設置xuanzhuan
? ? self.mapView.rotateEnabled = NO;
}
?
?
?
#pragma mark -MKMapViewDelegate
/**
?*? 每次添加大頭針 都會調用此方法
?*
?*? @param mapView? ? 地圖
?*? @param annotation 大頭針模型
?*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
?? ?
? ? LogYellow(@"%@",annotation);
? ? // 對用戶當前位置的大頭針, 特殊處理 - MKUserLocation
? ? if(![annotation isKindOfClass:[PPAnnotation class]]){
? ? ? ? return nil;
? ? }
? ? // 1. 創建大頭針
? ? PPAnnotationView *annoView = [PPAnnotationView annotationViewWithMap:mapView];
?? ?
? ? // 2. 設置模型
? ? annoView.annotation = annotation;
?? ?
?? ?
? ? // 3. 返回大頭針
? ? return annoView;
}
?
?
/**
?*? 地圖區域改變完成 會調用此方法
?*/
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
? ? NSLog(@"地圖區域改變完成");
? ? /**
?? ? *
?? ? CLLocationCoordinate2D center;
?? ? MKCoordinateSpan span;
?? ? */
? ? LogRed(@"%f --- %f",self.mapView.region.span.latitudeDelta,? self.mapView.region.span.longitudeDelta);
?? ?
}
?
?
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
? ? [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
?? ? ? ?
? ? ? ? CLPlacemark *placemark = [placemarks firstObject];
?? ? ? ?
? ? ? ? userLocation.title = placemark.name;
? ? ? ? userLocation.subtitle? = placemark.locality;
?? ? ? ?
? ? }];
?? ?
? ? // 移動地圖到當前用戶所在位置
? ? [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
?? ?
? ? /*
? ? // 設置地圖顯示的區域
? ? CLLocationCoordinate2D center = userLocation.location.coordinate;
? ? // 指定經緯度的跨度
? ? MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.0001);
?? ?
? ? // 將用戶的當前位置 設置為中心點, 并且制定顯示的跨度
? ? MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
? ? [self.mapView setRegion:region animated:YES];
?? ? */
?? ?
?? ?
}
轉載于:https://www.cnblogs.com/guangleijia/p/4828544.html
總結
以上是生活随笔為你收集整理的定位 - MapKit-自定义大头针的全部內容,希望文章能夠幫你解決所遇到的問題。