#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
{
MKMapView *mapView;
CLGeocoder *geocoder;
}
@end
@implementation ViewController
-(void)viewDidAppear:(BOOL)animated
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"是否开启定位" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
[alter show];
}
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.zoomEnabled = 1;
mapView.scrollEnabled = 1;
mapView.rotateEnabled = 1;
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
[self.view addSubview:mapView];
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(PressG:)];
[self.view addGestureRecognizer:longP];
geocoder = [[CLGeocoder alloc] init];
[self locationLatitude:23 and:112];
}
- (void)locationLatitude:(CGFloat)latitude and:(CGFloat)longitude
{
CLLocationCoordinate2D center = {latitude, longitude};
MKCoordinateSpan span;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
MKCoordinateRegion region = {center, span};
[mapView setRegion:region animated:1];
MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
ann.title = @"广东";
ann.subtitle = @"福地";
ann.coordinate = center;
[mapView addAnnotation:ann];
}
- (void)PressG:(UILongPressGestureRecognizer *)sender
{CGPoint pos = [sender locationInView:mapView];
// 将长按点的坐标转换为经度、维度值
CLLocationCoordinate2D center = [mapView convertPoint:pos toCoordinateFromView:mapView];
// 将经度、维度值包装为CLLocation对象
CLLocation *location = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];
// 根据经、纬度反向解析地址
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if ( error == nil)
{
// 获取解析得到的第一个地址信息
CLPlacemark* placemark = [placemarks objectAtIndex:0];
// 获取地址信息中的FormattedAddressLines对应的详细地址
NSArray* addrArray = placemark
.addressDictionary[@"FormattedAddressLines"];
// 将详细地址拼接成一个字符串
NSMutableString* address = [[NSMutableString alloc] init];
for(int i = 0 ; i < addrArray.count ; i ++)
{
[address appendString:addrArray[i]];
}
// 创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.title = placemark.name;
annotation.subtitle = address;
annotation.coordinate = center;
// 添加锚点
[mapView addAnnotation:annotation];
}
}];
}
- (MKAnnotationView *) mapView:(MKMapView *)mmapView
viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString* annoId = @"fkAnno";
// 获取可重用的锚点控件
MKAnnotationView* annoView = [mmapView
dequeueReusableAnnotationViewWithIdentifier:annoId];
// 如果可重用的锚点控件不存在,创建新的可重用锚点控件
if (!annoView)
{
annoView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];
}
// 为锚点控件设置图片
annoView.image = [UIImage imageNamed:@"11.png"];
// 设置该锚点控件是否可显示气泡信息
annoView.canShowCallout = YES;
// 定义一个按钮,用于为锚点控件设置附加控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// 为按钮绑定事件处理方法
[button addTarget:self action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// 可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件
annoView.rightCalloutAccessoryView = button;
return annoView;
}