#import "ViewController.h"
#import <MapKit/MapKit.h> //地圖
#import <CoreLocation/CoreLocation.h> // 定位
@interface ViewController ()<MKMapViewDelegate>
{
CLGeocoder *_geo;//根據經緯度,反向解析地址
}
@property (nonatomic, strong) MKMapView *mapview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化 geo 物件
_geo = [[CLGeocoder alloc]init];
self.mapview = [[MKMapView alloc]initWithFrame:self.view.bounds];
self.mapview.delegate = self;
[self showPointOfLatitude:30.659462 longitude:104.065735];
[self.view addSubview:self.mapview];
UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.mapview addGestureRecognizer:gest];
}
- (void)showPointOfLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
//中心點(結構體)
CLLocationCoordinate2D center = {latitude,longitude};
//縮放比例(結構體)
MKCoordinateSpan span ;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
MKCoordinateRegion region = {center,span};
//設定地圖的中心點和縮放比例 setRegion
[self.mapview setRegion:region animated:YES];
#pragma mark -- 設定點的資訊
MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
anno.title = @"四川科技館";
anno.subtitle = @"天府廣場附近的一點";
anno.coordinate = center;
//呼叫addAnnotation 方法去給地圖上加點
[self.mapview addAnnotation:anno];
}
#pragma mark -- 點選事件
- (void)detailButtonClick:(UIButton *)sender {
}
//長按手勢
- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
//獲取常按點
CGPoint point = [sender locationInView:self.mapview];
//通過地圖方法 convertPoint: toCoordinateFromView: 把點轉換成經緯度
CLLocationCoordinate2D coord = [self.mapview convertPoint:point toCoordinateFromView:self.mapview];
//組裝CLLocation 引數
CLLocation *location = [[CLLocation alloc]initWithLatitude:coord.latitude longitude:coord.longitude];
[_geo reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0 && error == nil) {
CLPlacemark *placemark = placemarks[0];
NSArray *addArray = placemark.addressDictionary[@"FormattedAddressLines"];
//講一個詳細的地址轉換成字串
NSMutableString * address = [[NSMutableString alloc]init];
for (int i = 0; i < addArray.count; i ++) {
[address appendString: addArray[i]];
}
//設定點
MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
anno.title = placemark.name;
anno.subtitle = address;
anno.coordinate = coord;
[self.mapview addAnnotation:anno];
}
}];
}
#pragma mark -- MKMapViewDelegate 協議方法
//自定義大頭針的方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *annoID = @"annoId";
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoID];
if (!annoView) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoID];
}
//設定大頭針的頭
annoView.image = [UIImage imageNamed:@"F9F748E09B377F4B56DDFE0977FA40DE.gif"];
//允許點選大頭針彈窗顯示詳情
annoView.canShowCallout = YES;
UIButton *detailButton= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton addTarget:self action:@selector(detailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
annoView.rightCalloutAccessoryView = detailButton;
//返回自定義大頭針
return annoView;
}