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

相关文章

ScrollView 往上滑动,里面的一个View停在某个位置的思路

1.scrollView的contentoffset 为view的左上角,减去此时scrollView的左上角 2.而且还不需要让那个红色的view removeFromSuperView ,直接self.view AddSubView 就会自动从原来的那个View脱离开来 3.以后遇到问题的思路。当发现UIView很许多奇特的效果的时候,思考它是不是在不断的改变父控件。 #pragma m

导航条下 ScrollView 第一个View去掉向下偏移的64px

self.automaticallyAdjustsScrollViewInsets=NO;      self.scrollView.contentInset=UIEdgeInsetsMake(-64, 0, 0, 0); self.automaticallyAdjustsScrollViewInsets=NO; 相同的意思。

android自定义View的和FramgentActivity的一个小坑

对于自定义View //加载样式TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView, defStyleAttr, 0);setTitle(typedArray.getString(R.styleable.TitleBarView_main_title));//不能写成

Java compiler level does not match the version of the installed Java project facet. map解决方法

右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面。 在页面中的“Java”下拉列表中,选择相应版本就OK了。

算法13—Bit Map算法简介

1. Bit Map算法简介          来自于《编程珠玑》。所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省。 2、 Bit Map的基本思想         我们先来看一个具体的例子,假设我们要对0-7内的5个元素(4,7,2,5,3)排

Android自定义View学习笔记03

Android自定义View学习笔记03 参考gitHub上面的开源项目CircleImageView 预备知识 BitMap类 BitMap位图类,其中有一个嵌套类叫Bitmap.Config,内部有四个枚举值。这个类的作用是定义位图存储质量,即存储一个像素的位数,以及是否能显示透明、半透明颜色(Possible bitmap configurations. A bitmap co

Android自定义view学习笔记02

Android自定义view学习笔记02 本文代码来自于张鸿洋老师的博客之Android 自定义View (二) 进阶 学习笔记,对代码进行些许修改,并补充一些在coding过程中遇到的问题、学习的新东西。 相关代码 //CustomImageView.javapackage mmrx.com.myuserdefinedview.textview;import android.con

Android自定义view学习笔记01

Android自定义view学习笔记01 昨天看博客的时候看到鸿洋老师的博客里面有关于自定义view的学习教程。一直深感所掌握的东西太少太杂,按照他的Android 自定义View (一)所讲内容,代码实践。根据实际情况稍作修改,并且补充一些在代码过程中知识点,做此笔记。 相关代码 //CustomView01.javapackage mmrx.com.myuserdefinedvi

【Android面试八股文】在 Android 的 View 分发机制中有哪些反向制约的方法?

文章目录 一、在 Android 的 View 分发机制中有哪些反向制约的方法?1.1 `requestDisallowInterceptTouchEvent` 方法1.1.1 源码分析 1.2 事件回调方法中干预父 View 的行为1.2.1 示例代码 1.3 总结 一、在 Android 的 View 分发机制中有哪些反向制约的方法? 在 Android 的 View 分

玩转Web之Json(四)---json与(Object/List/Map)的相互转化

在做web应用时,经常需要将json转化成Object/list/map或者将Object/List/map转化成json,通过简单封装可以在写代码是减轻很多负担。本文将给出json转化的一系列方法。 闲话不 多说,直接上代码: 先是Object /List /Map转化为Json /* 功能 :将一个对象转成json数组* 参数 :object对象* retu