美文网首页
地图定位

地图定位

作者: 张家杰仔 | 来源:发表于2017-04-06 10:40 被阅读47次
#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;
}

相关文章

  • 基于fabric的地图定位,SVG热力地图

    基于fabric的地图定位,SVG热力地图 基于fabric的地图定位,SVG热力地图 基于 fabricjs v...

  • 地图定位的不显示

    苹果自带地图定位功能 地图定位 今天要做苹果自带地图定位功能,基于mapkit框架的。怎么也没有找到定位自己的位置...

  • iOS地图和定位

    iOS地图定位 本文发布在http://he8090.cn/2016/07/18/地图与定位/ 导入地图框架 1、...

  • IOS地图定位导航

    title : IOS地图定位导航category : UI 地图定位导航 标签(空格分隔): IOS 概述 I...

  • 地图显示踩坑

    问题一:为什么定位点不在地图正中间? 应该先显示地图div,再画地图画地图时,先获取当前定位的坐标,再画地图 问题...

  • 地图与定位

    OCiOS开发:地图与定位 - 李鴻耀 - 博客频道 - CSDN.NET iOS开发之地图-定位/...

  • 高德地图,获取定位的过程中已经打开权限还是提示没有权限

    使用高德地图,获取定位的过程中,出现以下问题: //地图错误: [ #OnLocationChanged ] 定位...

  • 地图定位

    这个功能主要实现的实时定位 1.注意点 info.plist添加两个文件 2.实现代码

  • 地图定位

    定位使用 " CoreLocation 框架 想要定位,需要使用5个步骤 1.首先创建一个"强引用"的位置管理器C...

  • 地图定位

    #import #import { //定义变量地图视图...

网友评论

      本文标题:地图定位

      本文链接:https://www.haomeiwen.com/subject/woytattx.html