定位 - MapKit-自定义大头针
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PPAnnotation : NSObject<MKAnnotation>
/**
* 大头针的位置
*/
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/**
* 大头针标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 大头针的子标题
*/
@property (nonatomic, copy) NSString *subtitle;
/**
* 图标
*/
@property (nonatomic, copy) NSString *icon;
@end
------------------------------------------------------------------------------------------------------------------
#import <MapKit/MapKit.h>
@interface PPAnnotationView : MKAnnotationView
/**
* 快速创建 大头针
*
* @param mapView mapView
*/
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView;
@end
#import "PPAnnotationView.h"
#import "PPAnnotation.h"
@implementation PPAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
// 初始化
// 设置显示标题
self.canShowCallout = YES;
// 设置辅助视图
self.leftCalloutAccessoryView = [[UISwitch alloc] init];
self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
}
return self;
}
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView
{
static NSString *ID = @"anno";
// 1. 从缓存中取
// [注意] 默认情况下, MKAnnotationView 是无法显示的, 如果想自定义大头针, 需要使用MKAnnotationView的子类 MKPinAnnotationView
// [注意] 如果是自定义的大头针, 默认情况点击大头针不会显示标题和副标题, 需要手动设置显示
// MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
PPAnnotationView *annoView = (PPAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
// 2. 如果缓存中没有, 创建一个新的大头针
if (annoView == nil) {
annoView = [[PPAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
}
return annoView;
}
//- (void)setAnnotation:(id<MKAnnotation>)annotation
- (void)setAnnotation:(PPAnnotation *)annotation
{
[super setAnnotation:annotation];
// 处理自己特有的操作
self.image = [UIImage imageNamed:annotation.icon];
}
@end
---------------------------------------------------------------------------------------------
#import "ViewController.h"
//#import <MapKit/MapKit.h>
#import "PPAnnotation.h"
#import "PPAnnotationView.h"
@interface ViewController ()<MKMapViewDelegate>
/**
* 地图
*/
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation ViewController
/**
* 添加大头针
*/
- (IBAction)addAnnotation:(id)sender {
PPAnnotation * annotation = [[PPAnnotation alloc] init];
annotation.title = @"北京智德创辉网络科技有限公司";
annotation.subtitle = @"RO";
annotation.icon = @"category_4";
CGFloat latitude = 39.915094 + arc4random_uniform(20);
CGFloat longitude = 116.487775 + arc4random_uniform(20);
annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.mapView addAnnotation:annotation];
}
- (CLLocationManager *)mgr{
if (!_mgr) {
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
}
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
// ios 8
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
[self.mgr requestAlwaysAuthorization];
}
// 设置代理
self.mapView.delegate = self;
// 设置模式
self.mapView.mapType = MKMapTypeStandard;
// 设置跟踪
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
// 设置xuanzhuan
self.mapView.rotateEnabled = NO;
}
#pragma mark -MKMapViewDelegate
/**
* 每次添加大头针 都会调用此方法
*
* @param mapView 地图
* @param annotation 大头针模型
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
LogYellow(@"%@",annotation);
// 对用户当前位置的大头针, 特殊处理 - MKUserLocation
if(![annotation isKindOfClass:[PPAnnotation class]]){
return nil;
}
// 1. 创建大头针
PPAnnotationView *annoView = [PPAnnotationView annotationViewWithMap:mapView];
// 2. 设置模型
annoView.annotation = annotation;
// 3. 返回大头针
return annoView;
}
/**
* 地图区域改变完成 会调用此方法
*/
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"地图区域改变完成");
/**
*
CLLocationCoordinate2D center;
MKCoordinateSpan span;
*/
LogRed(@"%f --- %f",self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
userLocation.title = placemark.name;
userLocation.subtitle = placemark.locality;
}];
// 移动地图到当前用户所在位置
[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
/*
// 设置地图显示的区域
CLLocationCoordinate2D center = userLocation.location.coordinate;
// 指定经纬度的跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.0001);
// 将用户的当前位置 设置为中心点, 并且制定显示的跨度
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
*/
}
定位 - MapKit-自定义大头针的更多相关文章
- (八十)MapKit放置系统默认大头针和自定义大头针
有关MapView的显示和定位在上一节已经说明,这一节说明如何在地图上放置大头针,以及设置点击大头针的视图. [系统默认大头针] mapView上放置大头针的方法是调用其addAnnotation:方 ...
- iOS进阶_地图上定位的标志——大头针
一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...
- iOS地图多个自定义大头针绘制核心代码
首先需要自定义一个包含经纬度,title,subtitle的数据模型 #import <Foundation/Foundation.h> #import <MapKit/MapKit ...
- 百度地图jsapi 自定义大头针的方法
百度地图jsapi 自定义大头针的方法<pre> var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdem ...
- MapKit 添加大头针
#import "ViewController.h" #import <MapKit/MapKit.h> #import "MYAnnotation.h&qu ...
- 定位 - MapKit - 基本使用
/** * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Co ...
- iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)
大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...
- 【iOS】7.4 定位服务->3.1 地图框架MapKit 功能1:地图展示
> 本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. --- > 本文相关目录: ================== 所属文集:[[ ...
- iOS- 用MapKit和CoreLocation 来实现移动设备(地图与定位)
1.前言 发现在很多的社交软件都引入了地图和定位功能,如果我们要想实现这两大功能,需要利用到两个框架:MapKit和CoreLocation 我们先来看看CoreLocation框架: 它可以 ...
随机推荐
- Linux--------------安装vim
1.相关提示 -bash: vim: command not found 2.查看vim是否安装 rpm -qa|grep vim vim-en ...
- Margin和Padding之间的区别
margin ,padding body他们之间的区别就是 margin表示的是外边框的距离 padding表示的是内边框的距离 body表示的边框的距离
- gdal和python在windows上的安装
GDAL is a useful command line tool to process spatial data, if you haven’t heard of the tool before ...
- SignalR: The new old thing
As you can see, this is my first blog posted in cnblog. If you find any mistake, don’t hesitate to t ...
- java中数据类型及运算符的注意事项
数据类型: boolean 类型数据只允许取值true 或 false(不可以使用0 或非0的整数来代替true和false,区分于C语言). char:Java中用" \u四位十六进制的数 ...
- php有三种工作模式
php有三种工作模式. 其中是最常见的是php作为一个模块工作在一个多进程的webserver中, 例如apache webserver. apache会启动一个主进程, 多个子进程(php). 主进 ...
- linux关闭声音
对于CentOS/Redhat/RHEL/Fedora系统,使用root身份执行:echo "alias pcspkr off" >> /etc/modprobe.co ...
- Javascript中数组方法汇总
Array.prototype中定义了很多操作数组的方法,下面介绍ECMAScript3中的一些方法: 1.Array.join()方法 该方法将数组中的元素都转化为字符串并按照指定符号连接到一起,返 ...
- C++类继承内存布局(一)
转自:http://blog.csdn.net/jiangyi711/article/details/4890889# 一 类布局 不同的继承方式将导致不同的内存布局 1)C结构 C++基于C,所以C ...
- 【ADO.NET】8、DataSet的使用
一.DataSet的查询 SqlDataReader 适用于大型数据的读取,它是一条一条的读取,读取出来的数据是存放在服务器上当正在读取数据的时候,突然与服务中断,将无法读取后面的数据DataSet ...