7.5 Displaying Pins with Different Colors on a Map View

2024-03-01 10:18

本文主要是介绍7.5 Displaying Pins with Different Colors on a Map View,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


自定义pin颜色

MyAnnotation.h文件


#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>


#define REUSABLE_PIN_RED @"Red"

#define REUSABLE_PIN_GREEN @"Green" 

#define REUSABLE_PIN_PURPLE @"Purple"


@interface MyAnnotation : NSObject <MKAnnotation>

/* unsafe_unretained since this is not an object. We can skip this and leave it to the compiler to decide. weak or strong won't work as this is not an object */

@property (nonatomic, unsafe_unretained, readonly) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

/* unsafe_unretained for the same reason as the coordinate property */

@property (nonatomic, unsafe_unretained) MKPinAnnotationColor pinColor;


- (id) initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates

                     title:(NSString*)paramTitle

                  subTitle:(NSString*)paramSubTitle;


+ (NSString *) reusableIdentifierforPinColor:(MKPinAnnotationColor)paramColor;

@end



MyAnnotation.m文件


#import "MyAnnotation.h"


@implementation MyAnnotation


+ (NSString *) reusableIdentifierforPinColor :(MKPinAnnotationColor)paramColor

{

    

    NSString *result = nil;

    switch (paramColor){

        case MKPinAnnotationColorRed:{

            result = REUSABLE_PIN_RED;

            break;

        }

        case MKPinAnnotationColorGreen:{

            result = REUSABLE_PIN_GREEN;

            break;

        }

        case MKPinAnnotationColorPurple:{

            result = REUSABLE_PIN_PURPLE;

            break;

        }

    }

    return result;

}


- (id) initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates

                     title:(NSString*)paramTitle

                  subTitle:(NSString*)paramSubTitle

{

    self = [super init];

    if (self != nil)

    {

        _coordinate = paramCoordinates;

        _title = paramTitle;

        _subtitle = paramSubTitle;

        _pinColor = MKPinAnnotationColorGreen;

    }

    return self;

}

@end





ViewController.h文件

#import <UIKit/UIKit.h>

@protocol MKMapViewDelegate;


@interface ViewController : UIViewController<MKMapViewDelegate>


@end



ViewController.m文件


#import <MapKit/MapKit.h>

#import "ViewController.h"

#import "MyAnnotation.h"


@interface ViewController ()

@property (nonatomic, retain) MKMapView * myMapView;


@end


@implementation ViewController


- (MKAnnotationView *)mapView:(MKMapView *)mapView

            viewForAnnotation:(id <MKAnnotation>)annotation

{

    MKAnnotationView *result = nil;

    if ([annotation isKindOfClass:[MyAnnotation class]] == NO){

        return result;

    }

    if ([mapView isEqual:self.myMapView] == NO){

        /* We want to process this event only for the Map View

         that we have created previously */

        return result;

    }

    /* First typecast the annotation for which the Map View has fired this delegate message */

    MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;

    /* Using the class method we have defined in our custom annotation class, we will attempt to get a reusable identifier for the pin we are about

     to create */

    NSString *pinReusableIdentifier =[MyAnnotation reusableIdentifierforPinColor:senderAnnotation.pinColor];

    /* Using the identifier we retrieved above, we will attempt 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 of each pin in case we have assigned title and/or subtitle to each pin */

        [annotationView setCanShowCallout:YES];

    }

  

    /* Now make sure, whether we have reused a pin or not, that the color of the pin matches the color of the annotation */

    annotationView.pinColor = senderAnnotation.pinColor;

    result = annotationView;

    return result;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    /* Create a map as big as our view */

    self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    self.myMapView.delegate = self;

    /* Set the map type to Standard */

    self.myMapView.mapType = MKMapTypeStandard;

    self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    /* Add it to our view */

    [self.view addSubview:self.myMapView];

    /* This is just a sample location */

    CLLocationCoordinate2D location;

    location.latitude = 24.5;

    location.longitude = 118.13;//厦门

    /* Create the annotation using the location */

    MyAnnotation *annotation =

    [[MyAnnotation alloc] initWithCoordinates:location

                                        title:@"My Title"

                                     subTitle:@"My Sub Title"];

    annotation.pinColor = MKPinAnnotationColorPurple;

    /* And eventually add it to the map */

    [self.myMapView addAnnotation:annotation];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


输出:



这篇关于7.5 Displaying Pins with Different Colors on a Map View的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/762095

相关文章

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

MVC(Model-View-Controller)和MVVM(Model-View-ViewModel)

1、MVC MVC(Model-View-Controller) 是一种常用的架构模式,用于分离应用程序的逻辑、数据和展示。它通过三个核心组件(模型、视图和控制器)将应用程序的业务逻辑与用户界面隔离,促进代码的可维护性、可扩展性和模块化。在 MVC 模式中,各组件可以与多种设计模式结合使用,以增强灵活性和可维护性。以下是 MVC 各组件与常见设计模式的关系和作用: 1. Model(模型)

MFC中App,Doc,MainFrame,View各指针的互相获取

纸上得来终觉浅,为了熟悉获取方法,我建了个SDI。 首先说明这四个类的执行顺序是App->Doc->Main->View 另外添加CDialog类获得各个指针的方法。 多文档的获取有点小区别,有时间也总结一下。 //  App void CSDIApp::OnApp() {      //  App      //  Doc     CDocument *pD

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.B

一个bug日志 FATAL EXCEPTION: main03-25 14:24:07.724: E/AndroidRuntime(4135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syyx.jingubang.ky/com.anguotech.android.activity.Init

自定义view中常用到哪些方法作用分别是什么

目录 构造函数onMeasure(int widthMeasureSpec, int heightMeasureSpec)onDraw(Canvas canvas)onLayout(boolean changed, int left, int top, int right, int bottom)onTouchEvent(MotionEvent event)onSizeChanged(int

Map

Map 是 Java 中用于存储键值对的集合接口。以下是对 Map 的详细介绍: 特点 键值对存储:每个元素包含一个键和一个值。 键唯一:键不能重复,但值可以重复。 无序/有序:根据具体实现,键值对的顺序可能无序(如 HashMap)或有序(如 TreeMap、LinkedHashMap)。 主要实现类 HashMap 基于哈希表,无序存储。 允许一个 null 键和多个 null 值。

Java中集合类Set、List和Map的区别

Java中的集合包括三大类,它们是Set、List和Map,它们都处于java.util包中,Set、List和Map都是接口,它们有各自的实现类。Set的实现类主要有HashSet和TreeSet,List的实现类主要有ArrayList,Map的实现类主要有HashMap和TreeMap。那么它们有什么区别呢? Set中的对象不按特定方式排序,并且没有重复对象。但它的有些实现类能对集合中的对

C++数据结构重要知识点(5)(哈希表、unordered_map和unordered_set封装)

1.哈希思想和哈希表 (1)哈希思想和哈希表的区别 哈希(散列、hash)是一种映射思想,本质上是值和值建立映射关系,key-value就使用了这种思想。哈希表(散列表,数据结构),主要功能是值和存储位置建立映射关系,它通过key-value模型中的key来定位数组的下标,将value存进该位置。 哈希思想和哈希表数据结构这两个概念要分清,哈希是哈希表的核心思想。 (2)unordered