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

相关文章

JavaScript中的Map用法完全指南

《JavaScript中的Map用法完全指南》:本文主要介绍JavaScript中Map用法的相关资料,通过实例讲解了Map的创建、常用方法和迭代方式,还探讨了Map与对象的区别,并通过一个例子展... 目录引言1. 创建 Map2. Map 和对象的对比3. Map 的常用方法3.1 set(key, v

Golang中map缩容的实现

《Golang中map缩容的实现》本文主要介绍了Go语言中map的扩缩容机制,包括grow和hashGrow方法的处理,具有一定的参考价值,感兴趣的可以了解一下... 目录基本分析带来的隐患为什么不支持缩容基本分析在 Go 底层源码 src/runtime/map.go 中,扩缩容的处理方法是 grow

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

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