[IOS地图开发系类]5、改变大头针MKPinAnnotationView的颜色
2019独角兽企业重金招聘Python工程师标准>>>
在第四步的基础上,我们试着改变下大头针的颜色
//
// MKPinAnnotationView.h
// MapKit
//
// Copyright (c) 2009-2012, Apple Inc. All rights reserved.
//#import <MapKit/MKAnnotationView.h>enum {MKPinAnnotationColorRed = 0,MKPinAnnotationColorGreen,MKPinAnnotationColorPurple
};
typedef NSUInteger MKPinAnnotationColor;@class MKPinAnnotationViewInternal;MK_CLASS_AVAILABLE(NA, 3_0)
@interface MKPinAnnotationView : MKAnnotationView
{
@privateMKPinAnnotationViewInternal *_pinInternal;
}@property (nonatomic) MKPinAnnotationColor pinColor;@property (nonatomic) BOOL animatesDrop;@end
从大头针的头文件中可以看到,它有一个颜色属性我们可以直接使用。
修改下CustomAnnotation.h和其实现文件,代码如下:
//
// CustomAnnotation.h
// LBS_002_mapview
//
// Created by liqun on 13-7-25.
// Copyright (c) 2013年 Block Cheng. All rights reserved.
//#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>#define PIN_RED @"Red"
#define PIN_GREEN @"Green"
#define PIN_PURPLE @"Purple"
@interface CustomAnnotation : NSObject<MKAnnotation>@property (nonatomic, assign, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy, readonly) NSString *title;
@property (nonatomic, copy, readonly) NSString *subtitle;
@property (nonatomic, unsafe_unretained) MKPinAnnotationColor pinColor;
- (id) initWithCoordinates:(CLLocationCoordinate2D)paramCoordinatestitle:(NSString *)paramTitlesubTitle:(NSString *)paramSubTitle;+ (NSString *) reusableIdentifierforPinColor :(MKPinAnnotationColor)paramColor;
@end
//
// CustomAnnotation.m
// LBS_002_mapview
//
// Created by liqun on 13-7-25.
// Copyright (c) 2013年 Block Cheng. All rights reserved.
//#import "CustomAnnotation.h"@implementation CustomAnnotation- (void)dealloc
{[_subtitle release];[_title release];[super dealloc];
}
- (id) initWithCoordinates:(CLLocationCoordinate2D)paramCoordinatestitle:(NSString *)paramTitlesubTitle:(NSString *)paramSubTitle
{if (self = [super init]) {_coordinate = paramCoordinates;_title = [paramTitle copy];_subtitle = [paramSubTitle copy];}return self;
}+ (NSString *) reusableIdentifierforPinColor :(MKPinAnnotationColor)paramColor{ NSString *result = nil;switch (paramColor){case MKPinAnnotationColorRed:{result = PIN_RED;break;}case MKPinAnnotationColorGreen:{result = PIN_GREEN;break;}case MKPinAnnotationColorPurple:{result = PIN_PURPLE;break;}}return result;
}
@end
最后,在ViewControlelr实现文件中,加上该注解类的颜色属性:
//
// ViewController.m
// LBS_002_mapview
//
// Created by liqun on 13-7-25.
// Copyright (c) 2013年 Block Cheng. All rights reserved.
//#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "CustomAnnotation.h"@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (nonatomic,retain)MKMapView* mapView;
@property (nonatomic,retain)CLLocationManager* locationManager;
@property (nonatomic,retain)CLLocation* location;
@property (nonatomic, retain) CLGeocoder *myGeocoder;@end@implementation ViewController- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.MKMapView* map = [[MKMapView alloc] initWithFrame:self.view.frame];map.mapType = MKMapTypeStandard;map.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;[self.view addSubview:map];[map release];self.mapView = map;self.mapView.delegate = self;UIButton *addBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];addBt.frame = CGRectMake(0, 00, 320, 50);[addBt setTitle:@"locationMe" forState:UIControlStateNormal];[ addBt addTarget:self action:@selector(handleLocationMe:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview: addBt];if ([CLLocationManager locationServicesEnabled]){self.locationManager = [[CLLocationManager alloc] init];self.locationManager.delegate = self;self.locationManager.purpose =@"To provide functionality based on user's current location.";[self.locationManager startUpdatingLocation];} else {/* Location services are not enabled.Take appropriate action: for instance, prompt the user to enable location services */NSLog(@"Location services are not enabled");}self.myGeocoder = [[CLGeocoder alloc] init];}- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{MKAnnotationView *result = nil;if ([annotation isKindOfClass:[CustomAnnotation class]] == NO){return result;}if ([mapView isEqual:self.mapView] == NO){/* We want to process this event only for the Map Viewthat we have created previously */return result;}/* First, typecast the annotation for which the Map View hasfired this delegate message */CustomAnnotation *senderAnnotation = (CustomAnnotation *)annotation;/* Using the class method we have defined in our customannotation class, we will attempt to get a reusableidentifier for the pin we are aboutto create */NSString *pinReusableIdentifier =[CustomAnnotation reusableIdentifierforPinColor:senderAnnotation.pinColor];/* Using the identifier we retrieved above, we willattempt to reuse a pin in the sender Map View */MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinReusableIdentifier];if (annotationView == nil){/* If we fail to reuse a pin, then we will create one */annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:senderAnnotation reuseIdentifier:pinReusableIdentifier];/* Make sure we can see the callouts on top ofeach pin in case we have assigned title and/orsubtitle to each pin */[annotationView setCanShowCallout:YES];annotationView.draggable = YES;annotationView.enabled = YES;}/* Now make sure, whether we have reused a pin or created a new one,that the color of the pin matches the color of the annotation */annotationView.pinColor = senderAnnotation.pinColor;//自定义图片时,不能用dropannotationView.animatesDrop = YES;result = annotationView;return result;
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}-(IBAction)handleLocationMe:(id)sender
{[self.myGeocoderreverseGeocodeLocation:self.location completionHandler:^(NSArray *placemarks, NSError *error) {if (error == nil &&[placemarks count] > 0){CLPlacemark *placemark = [placemarks objectAtIndex:0];/* We received the results */NSLog(@"Country = %@", placemark.country);NSLog(@"Postal Code = %@", placemark.postalCode);NSLog(@"Locality = %@", placemark.locality);NSLog(@"dic = %@", placemark.addressDictionary );NSLog(@"dic FormattedAddressLines= %@", [placemark.addressDictionary objectForKey:@"FormattedAddressLines"]);NSLog(@"dic Name = %@", [placemark.addressDictionary objectForKey:@"Name"]);NSLog(@"dic State = %@", [placemark.addressDictionary objectForKey:@"State"]);NSLog(@"dic Street = %@", [placemark.addressDictionary objectForKey:@"Street"]);NSLog(@"dic SubLocality= %@", [placemark.addressDictionary objectForKey:@"SubLocality"]);NSLog(@"dic SubThoroughfare= %@", [placemark.addressDictionary objectForKey:@"SubThoroughfare"]);NSLog(@"dic Thoroughfare = %@", [placemark.addressDictionary objectForKey:@"Thoroughfare"]);CLLocationCoordinate2D location =CLLocationCoordinate2DMake(self.location.coordinate.latitude, self.location.coordinate.longitude);/* Create the annotation using the location */CustomAnnotation *annotation =[[CustomAnnotation alloc] initWithCoordinates:location title:placemark.name subTitle:placemark.thoroughfare];/* And eventually add it to the map */annotation.pinColor = MKPinAnnotationColorPurple;[self.mapView addAnnotation:annotation];[annotation release];}else if (error == nil &&[placemarks count] == 0){NSLog(@"No results were returned.");}else if (error != nil){NSLog(@"An error occurred = %@", error);}}];
}- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{/* We received the new location */NSLog(@"Latitude = %f", newLocation.coordinate.latitude);NSLog(@"Longitude = %f", newLocation.coordinate.longitude);self.location = newLocation;}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{/* Failed to receive user's location */
}- (void)dealloc
{self.mapView = nil;self.location = nil;self.locationManager = nil;[super dealloc];
}@end
运行后,效果如下:
转载于:https://my.oschina.net/chengliqun/blog/147915
[IOS地图开发系类]5、改变大头针MKPinAnnotationView的颜色相关推荐
- [IOS地图开发系类]2、位置解码CLGeocoder
2019独角兽企业重金招聘Python工程师标准>>> 接第一步的操作,获取到地址信息经纬度后,我们可以对其进行解码,解码采用的CLGeocoder这个类,使用方式如下: 1.在Vi ...
- java测试开发_测试开发系类之Java常用知识点
测试需要的两门语言:Java,Python 测试开发:开发测试脚本->开发测试框架 Java需要掌握内容:基础语法.Java面向对象相关概念.Java常用类.基础测试框架 Java常用类:IO相 ...
- 小程序开发系类之基础部分-开发工具
根据不同开发环境去下载对应的开发工具 http://dwz.cn/4hOfGk 笔者是用MAC版本,扫码登陆之后(扫码的前提需要完成小程序帐户的注册) 选择调试小程序 笔者是用MAC版本,选择调试小程 ...
- 测试开发系类之接口自动化测试
接口定义 代码角度的接口Interface 定义:Java中的接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现 ...
- iOS 高德地图开发详解
Demo地址 如果有所帮助记得关注,点Star demo中添加了查看路况功能,如果不需要,可以删除. ##一:基本地图功能实现 ####1.申请密钥流程 申请密钥链接 2.配置环境(重点) 高德地图提 ...
- iOS地图定位导航与大头针的简单使用
定位 1.一次定位 1.创建位置管理器 // 这里创建的管理其对象如果没有强引用,就会造成你后面的操作不会出现效果,全局变量强引用.CLLocationManager *manager = [[CLL ...
- iOS开发之利用苹果系统自带地图进行地图开发
了解更多关于移动开发,欢迎到悦卓3G孵化园:http://www.91train.com iOS中有一个系统自带的 完整的一套地图框架--MapKit.Framework和CoreLocation.F ...
- HBuilder开发旅游类APP(一) ----- 实现地图插件调用、苹果和安卓同步混合开发
HBuilder开发旅游类APP(一) ----- 实现地图插件调用.苹果和安卓同步混合开发 作者:班尼科 本博文内容参考了网络资源,但文章完全是本人原创,喜欢请给我点赞,转载请注明出处哦. 标签: ...
- 在iOS中进行Mapbox地图开发杂谈
最近因项目需要,在app中要集成Mapbox,并且要能与苹果地图切换使用.从最早认识Mapbox到最终集成完毕,中间有一些断断续续,但总的时间也不算短了,关于这方面的资料其实是比较少的,能参考的基本都 ...
最新文章
- python第一章测试题_第一章 测试【含答案】 Python大数据分析
- Win32 汇编环境和入门程序图解
- 机器学习面试知识点汇总(Machine Learning Core Concepts Collection)
- vue html绑定数组,VueJs Class 与 Style 绑定 数组语法
- 【有福如林】OpenFlashChart C#flash图形控件的使用
- ue4相机_纳格数字创意课程介绍 |UE4虚拟现实技术室内方向
- [MyBatis]DAO层只写接口,不用写实现类
- Dom4J 解析xml ,类查询
- AliRTC 开启视频互动 “零计算” 时代
- antd vue关闭模态对话框_Vue.extend 登录注册模态框
- mysql------事务
- php去除img,PHP如何去除IMG标签?_后端开发
- export project from intellij to myeclipse
- python怎么导入csv文件数据-机器学习Python实践——数据导入(CSV)
- win7 安装 vmware出错: failed to create the requested registry key key installer error 1021 的解决办法。...
- 【SpringBoot_ANNOTATIONS】组件注册 06 @Conditional 按照条件注册bean
- 智能影视站系统 光线 CMS1.5 正式版
- 对比Python,看看Excel如何3步给证件照换底色?
- Android博通BCM libbt-vendor.so 分析蓝牙初始化流程
- python 二维列表获取其中元素_Python中二维列表如何获取子区域元素的组成
热门文章
- node.js go java_ABAP,Java, nodejs和go语言的web server编程
- Spring 整合 Mybatis
- 输入一个正整数n,计算s=1-1/3+1/5-1/7…前n项之和
- 领扣简单版--两数之和(Two Sum)
- Octave 作图 无响应
- spring创建webservice项目
- NYOJ 745 dp
- 加速Java应用开发速度3——单元/集成测试+CI
- 我给女朋友讲编程html系列(1) -- Html快速入门
- 推荐JVM的9款编程语言杀手开发利器