Displaying Pins on a Map View

Problem

You want to point out a specific location on a map to the
user.

Solution

Use built-in map view annotations. Follow these steps:

1. Create a new class and call it MyAnnotation.

2.Make sure this class conforms to the MKAnnotation
protocol.

3.Define a property for this class of type
CLLocationCoordinate2D and name it coordinate. Make sure you set it as a
readonly property since the coordinate property is defined as readonly in the
MKAnnotation protocol.

4.Optionally, define two properties of type NSString, namely
title and subtitle, which will be able to carry the title and the subtitle
information for your annotation view. Both of these properties are readonly as
well.

5.Create an initializer method for your class that will accept a
parameter of type CLLocationCoordinate2D. In this method, assign the passed
location parameter to the property that we defined in step 3. Since this
property is readonly, it cannot be assigned by code outside the scope of this
class. Therefore, the initializer of this class acts as a bridge here and allows
us to indirectly assign a value to this property. We will do the same thing for
the title and subtitle properties.

6.Instantiate the MyAnnotation class and add it to your map
using the addAnnota tion: method of the MKMapView class.

Discussion

As explained in this recipe’s Solution, we must create an object
that conforms to the MKAnnotation protocol and later instantiate this object and
pass it to the map to be displayed. We will declare the header of this object
like so:

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface WSYAnnotation : NSObject<MKAnnotation>

@property(nonatomic,readonly)CLLocationCoordinate2D
coordinate;

@property(nonatomic,copy,readonly)NSString * title;

@property(nonatomic,copy,readonly)NSString * subtitle;

-
(instancetype)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates

title:(NSString *)paramTitle

subTitle:(NSString *)paramSubTitle;

@end

The .m file of the MyAnnotation class sets up the class
to display location information as follows:

#import "WSYAnnotation.h"

@implementation WSYAnnotation

-(instancetype)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates
title:(NSString *)paramTitle subTitle:(NSString *)paramSubTitle

{

self = [self init];

if (self!=nil) {

_coordinate =
paramCoordinates;

_title =
paramTitle;

_subtitle =
paramSubTitle;

}

return self;

}

@end

Later, we will instantiate this class and add it to our map, for
instance, in the .m file of a view controller that creates and displays
a map view:

#import "WSYViewController.h"

#import <MapKit/MapKit.h>

#import "WSYAnnotation.h"

@interface WSYViewController ()<MKMapViewDelegate>

@property(nonatomic,strong)MKMapView * myMapView;

@end

@implementation WSYViewController

- (void)viewDidLoad

{

[super viewDidLoad];

//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 simple loaction

CLLocationCoordinate2D location =
CLLocationCoordinate2DMake(27, 117);

//create the annotation uisng the
location

WSYAnnotation * annotation = [[WSYAnnotation
alloc]initWithCoordinates:location title:@"my title" subTitle:@"My sub
Title"];

//add eventually add it to the map

[self.myMapView
addAnnotation:annotation];

}

@end

Displaying Pins on a Map View,布布扣,bubuko.com

时间: 2024-10-24 06:34:34

Displaying Pins on a Map View的相关文章

Creating a Map View

Problem You want to instantiate and display a map on a view Solution Create an instance of the MKMapView class and add it to a view or assign it as a subview of your view controller that creates an instance Of MKMapView and displays it full-screen on

AngularJS - Map View

AngularJS is a JavaScirpt web framework amied to make web apps simple to build and easy to maintain. Files =================================================== CSS - main.css img - program-or-be-programmed.jpg - the-book-of-trees.jpg js controllers -

《iOS Human Interface Guidelines》——Map View

地图视图 地图视图展示地理数据并支持内置的地图app提供的大部分功能(如下图所示). API NOTE 查看MapKit Framework Reference学习更多关于在你的代码中定义地图视图的内容. 一个地图视图: 使用标准地图数据.卫星图或两者结合来显示地理区域 可以显示标记(标记简单的点)和覆盖物(描绘路径或者平面区域) 支持程序和用户控制的拉伸和拖动 使用地图视图来给用户一个地理区域的交互视图.如果你在开发一个路径app,就使用地图视图来显示用户的路线(查看Routing来获取更多创

springMVC返回map和返回json的例子

spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. Map   @RequestMapping("/demo2/show") public Map<String, String> getMap() { Map<String, String> map = new HashMap<String, String>(); map.put("key1&

View Controller容器

在 iOS 5 之前,view controller 容器是 Apple 的特权.实际上,在 view controller 编程指南中还有一段申明,指出你不应该使用它们.Apple 对 view controllers 的总的建议曾经是"一个 view controller 管理一个全屏幕的内容".这个建议后来被改为"一个 view controller 管理一个自包含的内容单元".为什么 Apple 不想让我们构建自己的 tab bar controllers

View拖拽 自定义绑定view拖拽的工具类

由于工作需求,需要用到这种处理方法所以我就写了这个 废话不多说先看效果图 接下来就看代码吧 DragDropManager import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.PixelFormat; import android.util.Log; import android.view.Gravity; impo

Learn UML with JUDE

我希望你能够使用JUDE去学习和体验UML,JUDE是一个建模工具,你可以用它去画UML.下面我会指导你通过一些实例去学习使用JUDE来画UML. 一.Overview l    UML and UML toolsl    Description of JUDEl    Installation of JUDEl    Fundamental Components and Basic Operation of JUDEl    UML about Bowlingl    Draw UseCase

【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等

转自:http://blog.csdn.net/dolacmeng/article/details/46594839 以前做项目用高德地图SDK,需要注册账号和AppID,然后下载SDK集成到项目中,比较麻烦,这几天看了下苹果自带的MapKit框架,感觉挺好用,官方文档也介绍得很详细,所以按照官方文档写了个demo,实现地图的显示.显示用户当前位置.放置大头针.弹出气泡等基本功能.希望能帮到刚接触MapKit的朋友~ 1.显示地图 (1)首先我们要像下图这样子打开地图功能: XCode会自动给我

MKMapView和MKMapViewDelegate

@interface MKMapView : UIView <NSCoding> @property (nonatomic, assign) id <MKMapViewDelegate> delegate; // Changing the map type or region can cause the map to start loading map content. // The loading delegate methods will be called as map co