美文网首页
地图定位

地图定位

作者: 9898a | 来源:发表于2017-09-21 19:09 被阅读0次

//ViewController.m

import "ViewController.h"

import <MapKit/MapKit.h>

import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
{
//定义变量地图视图、定位管理对象、地图解析对象
MKMapView *mv;
CLLocationManager *manager;
CLGeocoder *geo;
}
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    //初始化地图视图
    mv = [[MKMapView alloc]initWithFrame:self.view.bounds];
    mv.mapType = MKMapTypeStandard;
    mv.zoomEnabled = YES;
    mv.rotateEnabled = YES;
    mv.scrollEnabled = YES;
    mv.showsUserLocation = YES;
    [self.view addSubview:mv];
    //定位
    CLLocationCoordinate2D center = {34,101};
    MKCoordinateSpan span = {0.01,0.01};
    MKCoordinateRegion region = {center,span};
    [mv setRegion:region animated:YES];
    //初始化定位管理器
    manager = [[CLLocationManager alloc]init];
    [manager requestWhenInUseAuthorization];
    manager.delegate = self;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    manager.distanceFilter = kCLDistanceFilterNone;
    [manager startUpdatingLocation];
    //初始化地址解析对象
    geo = [[CLGeocoder alloc]init];
    }

//定位更新响应方法

  • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
    //获取当前位置
    CLLocation *loc = [locations lastObject];
    //定位
    CLLocationCoordinate2D center = {loc.coordinate.latitude,loc.coordinate.longitude};
    MKCoordinateSpan span = {0.01,0.01};
    MKCoordinateRegion region = {center,span};
    [mv setRegion:region animated:YES];
    NSMutableString *mSt = [NSMutableString string];
    //反向解析经纬度获取地址
    [geo reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    CLPlacemark *pla = [placemarks firstObject];

      NSArray *arr = [pla.addressDictionary objectForKey:@"FormattedAddressLines"];
      //拼接地址
      for (NSString *str in arr) {
          [mSt appendString:str];
      }
      //添加锚点
      MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
      anno.coordinate = center;
      anno.title = @"我的位置";
      anno.subtitle = mSt;
      [mv addAnnotation:anno];
    

    }];
    }

  • (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    CLLocationCoordinate2D loc = [userLocation coordinate];
    //放大地图到自身的经纬度位置。
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [mv setRegion:region animated:YES];
    }

  • (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
    static NSString *cellid = @"cellid";
    MKAnnotationView *anno = [mapView dequeueReusableAnnotationViewWithIdentifier:cellid];
    if (!anno) {
    anno = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:cellid];
    }
    anno.canShowCallout = YES;
    return anno;
    }

相关文章

  • 基于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/ojytextx.html