地圖-圖釘視圖,地圖圖釘視圖
//// MKMapViewController.m// MKMapView//// Created by xiaoyao on 15/3/25.// Copyright (c) 2015年 lije. All rights reserved.//#import "MKMapViewController.h"#import <CoreLocation/CoreLocation.h>#import <MapKit/MapKit.h>#import "CAnnotation.h"@interface MKMapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate> { MKMapView *_mapView; CLLocationManager *_locationManager;}@end@implementation MKMapViewController- (void)viewDidLoad { [super viewDidLoad] ; [self setUpUI];}- (void)setUpUI { CGRect rect = [UIScreen mainScreen].bounds; _mapView = [[MKMapView alloc] initWithFrame:rect]; _mapView.delegate = self; [self.view addSubview:_mapView]; if (![CLLocationManager locationServicesEnabled] || ![CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) { [_locationManager requestWhenInUseAuthorization]; _locationManager.delegate = self; [_locationManager startUpdatingLocation]; } // 設定 _mapView.mapType = MKMapTypeStandard; // 返回使用者追蹤軌跡,啟動定位 _mapView.userTrackingMode = MKUserTrackingModeFollow; CAnnotation *ca = [[CAnnotation alloc] init]; CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(36.56, 101.74); ca.title = @"lije"; ca.subtitle = @"lije’home"; ca.coordinate = coor; ca.image = [UIImage imageNamed:@"ca1"]; // 添加圖釘 [_mapView addAnnotation:ca]; CAnnotation *ca2 = [[CAnnotation alloc] init]; CLLocationCoordinate2D coor2 = CLLocationCoordinate2DMake(41.31, 121.51); ca2.title = @"zs"; ca2.subtitle = @"zs’home is jinzhou"; ca2.coordinate = coor2; ca2.image= [UIImage imageNamed:@"ca2"]; [_mapView addAnnotation:ca2];}#pragma mark - 地圖代理方法- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"%@",userLocation);}// 返回圖釘視圖- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[CAnnotation class]]) { static NSString *anotationKey = @"Annotation1"; MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:anotationKey]; // 如果緩衝池不存在則建立緩衝池對象 if (!annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:anotationKey]; annotationView.canShowCallout = true; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zs.jpeg"]]; annotationView.leftCalloutAccessoryView = imageView; annotationView.calloutOffset = CGPointMake(0, 1); } annotationView.annotation = annotation; annotationView.image = ((CAnnotation *)annotation).image; return annotationView; } else { // 如果返回nil則使用預設的圖釘視圖 return nil; }}@end//// CAnnotation.h// MKMapView//// Created by xiaoyao on 15/3/25.// Copyright (c) 2015年 lije. All rights reserved.//#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface CAnnotation : NSObject<MKAnnotation>/** * @brief 定製圖釘,重寫圖釘協議的三個屬性 */@property (nonatomic, copy) NSString *title; // 圖釘標題@property (nonatomic, copy) NSString *subtitle; // 副標題@property (nonatomic) CLLocationCoordinate2D coordinate; // 位置// 自訂圖釘圖片屬性@property (nonatomic, strong) UIImage *image;@end