iOS - MapKit地图的简单使用和定位
来源:互联网 发布:男士手提包知乎 编辑:程序博客网 时间:2024/05/04 09:02
按照前人的思路,简单修改了一下代码结构,自己整理了一个简单的地图展示器,可以定位当前位置,也可以插大头针
目前只是一个简单的demo而已,仅作记录用。
github路径:https://github.com/icefishlily/FairyFishMap
核心代码如下:
//// MapViewController.m// FairyFishMap//// Created by jinhui005 on 16/9/14.// Copyright © 2016年 yhl. All rights reserved.//#import "MapViewController.h"#import "MapKit/MapKit.h" //原生地图头文件#import "CoreLocation/CoreLocation.h" //核心定位服务头文件#import "CLLocation+Sino.h"@interface MapViewController () <MKMapViewDelegate, CLLocationManagerDelegate>@property (nonatomic, strong) MKMapView *mapView;@property (nonatomic, strong) CLLocationManager *locationManager;@property (nonatomic, strong) UISegmentedControl *segment;@end@implementation MapViewController- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.mapView]; [self.view addSubview:self.segment]; CGRect rect = [UIScreen mainScreen].bounds; self.mapView.frame = CGRectMake(0, 0, rect.size.width, rect.size.height); self.segment.frame=CGRectMake(30,30,180,30); CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; if (kCLAuthorizationStatusNotDetermined == status) { [self.locationManager requestAlwaysAuthorization]; } [self.locationManager startUpdatingLocation];}- (BOOL)prefersStatusBarHidden { return YES;}#pragma mark - 交互- (void)longPress:(UILongPressGestureRecognizer *)sender { if (sender.state != UIGestureRecognizerStateBegan) { return; } CGPoint longPressPoint = [sender locationInView:self.mapView]; CLLocationCoordinate2D coordinate2d = [self.mapView convertPoint:longPressPoint toCoordinateFromView:self.mapView]; //添加大头针 MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init]; pointAnnotation.coordinate = coordinate2d; pointAnnotation.title = @"我在这里"; pointAnnotation.subtitle = @"使用这个位置"; [self.mapView addAnnotation:pointAnnotation]; MKCircle *circle =[MKCircle circleWithCenterCoordinate:coordinate2d radius:20]; //先添加,在回调方法中创建覆盖物 [_mapView addOverlay:circle]; }//大头针的回调方法(与cell的复用机制很相似)-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{ //复用 MKPinAnnotationView *annotationView =(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"]; //判断复用池中是否有可用的 if(annotationView==nil) { annotationView =(MKPinAnnotationView *)[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"]; } //添加左边的视图 UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arraw"]]; imageView.frame=CGRectMake(0,0,50,50); annotationView.leftCalloutAccessoryView=imageView; //显示 annotationView.canShowCallout=YES; //设置是否显示动画 annotationView.animatesDrop=YES; //设置右边视图 UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0,0,30,30)]; label.text=@">>"; annotationView.rightCalloutAccessoryView=label; //设置大头针的颜色 annotationView.pinColor = MKPinAnnotationColorRed; return annotationView; }//覆盖物的回调方法-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{ //创建圆形覆盖物 MKCircleRenderer *circleRender =[[MKCircleRenderer alloc] initWithCircle:overlay]; //设置边缘颜色 circleRender.strokeColor=[UIColor grayColor]; return circleRender; }//解决手势冲突,可以同时使用多个手势- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}- (void)mapTypeChanged:(UISegmentedControl *)sender { self.mapView.mapType = sender.selectedSegmentIndex;}#pragma mark - getter setter-(MKMapView *)mapView { if (nil == _mapView) { _mapView = [[MKMapView alloc] init]; _mapView.delegate = self; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [_mapView addGestureRecognizer:longPress]; } return _mapView;}-(CLLocationManager *)locationManager { if (nil == _locationManager) { _locationManager = [[CLLocationManager alloc] init]; //设置定位属性 _locationManager.desiredAccuracy = kCLLocationAccuracyBest; //设置定位更行距离 米 _locationManager.distanceFilter = 10.0; _locationManager.delegate = self; } return _locationManager;}-(UISegmentedControl *)segment { if (nil == _segment) { NSArray *array = @[@"标准", @"卫星", @"混合"]; _segment = [[UISegmentedControl alloc] initWithItems:array]; [_segment addTarget:self action:@selector(mapTypeChanged:) forControlEvents:UIControlEventValueChanged]; _segment.selectedSegmentIndex = 0; } return _segment;}#pragma mark - CLLocationmanagerDelegate//定位后的回调,返回结果- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { CLLocation *location = [locations firstObject]; //火星坐标转地球坐标 location=[location locationMarsFromEarth]; //设置地图显示经纬度的位置 MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01, 0.01)); [self.mapView setRegion:region animated:YES]; //创建大头针 MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init]; pointAnnotation.coordinate = location.coordinate; pointAnnotation.title = @"我在这里"; pointAnnotation.subtitle = @"这是什么鬼"; [self.mapView addAnnotation:pointAnnotation];}@end
0 0
- iOS - MapKit地图的简单使用和定位
- iOS --- 地图框架MapKit的简单使用
- 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
- IOS地图与定位MapKit
- [第1章]地图:MapKit的简单使用——定位
- iOS<MapKit/MapKit.h> MKMapView的简单使用
- IOS开发 使用地图 MapKit
- IOS开发 使用地图 MapKit
- IOS开发 使用地图 MapKit
- IOS开发 使用地图 MapKit
- IOS开发 使用地图 MapKit
- IOS学习之Core Location定位和MapKit视图使用
- 使用ios系统自带的MapKit framework进行定位
- iOS开发中地图(MapKit)的使用
- iOS开发中地图(MapKit)的使用
- IOS学习之——MapKit 地图的使用
- iOS开发中地图(MapKit)的使用
- iOS中 CoreLocation 和 MapKit 的使用
- 中国各大银行卡号查询
- 仿淘宝商品详细页(上拉阻尼效果以及第二页View Page滑动)
- 腾讯对象存储服务COS加密签名上传文件与下载文件的剖析,福利提供给所有使用Android的小伙伴们!
- STM32F4的system_stm32f4xx.c文件需要根据不同板卡进行修改
- RDD键值转换操作(2)–combineByKey、foldByKey
- iOS - MapKit地图的简单使用和定位
- listview左右滑动
- Android应用保活分析
- 单例模式
- linux服务器挂载新磁盘步骤
- UVA 10900
- Google 的开源技术protobuf 简介与例子
- SVM+MFCC在0-9单词识别应用示例
- MAC 下卸载 JAVA