#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;
}
地图定位
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转载、引用请标明出处http://www.jianshu.com/p/29ccac3e1e42本文出自zhh_ha...
- 最近项目用到高德地图,因此来写一篇文章理一下高德的使用步骤方法,希望对大家有用! 1.注册+配置 废话不多说,要使...
- 农历2017年就要来了,12生肖的运势也会随之变化,2017年丁酉鸡年纳音山下火。12生肖在新一年如何找对太岁位增...