十年专注于品牌网站建设 十余年专注于网站建设_小程序开发_APP开发,低调、敢创新、有情怀!
南昌百恒网络微信公众号 扫一扫关注
小程序
tel-icon全国服务热线:400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络微信小程序

百恒网络

南昌百恒网络

实现地图委托方法mapView:viewForAnnotation:

百恒网络 2017-09-19 4752

昨天南昌APP制作开发公司-百恒网络为大家介绍了使用IOS苹果地图添加标注的第一个步骤:触发添加动作,那么今天本公司就来为大家介绍第二步:实现地图委托方法 mapView:viewForAnnotation:,这也是最后一步!具体方法如下:

MKMapViewDelegate委托协议方法mapView:viewForAnnotation:的代码如下:

func mapView(mapView: MKMapView!, viewForAnnotation annotation:

MKAnnotation!) -> MKAnnotationView! { ①

var annotationView = self.mapView.dequeueReusableAnnotationViewWith

Identifier("PIN_ANNOTATION")

as? MKPinAnnotationView ②

if annotationView == nil { ③

annotationView = MKPinAnnotationView(annotation: annotation,

reuseIdentifier: "PIN_ANNOTATION") ④

}

annotationView!.pinColor = MKPinAnnotationColor.Purple ⑤

annotationView!.animatesDrop = true ⑥

annotationView!.canShowCallout = true ⑦

return annotationView!

}

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

viewForAnnotation:(id ) annotation { ①

MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView

dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"]; ②

if(annotationView == nil) { ③

annotationView = [[MKPinAnnotationView alloc]

initWithAnnotation: annotation

reuseIdentifier:@"PIN_ANNOTATION"]; ④

}

annotationView.pinColor = MKPinAnnotationColorPurple; ⑤

annotationView.animatesDrop = YES; ⑥

annotationView.canShowCallout = YES; ⑦

return annotationView;

}

在上述代码中,第①行代码所示的委托方法mapView:viewForAnnotation:在地图视图添加标注时回调。给地图视图添加标注的方法是self.mapView.addAnnotation(annotation),其中annotation是地图标注对象。

第②~④行代码用于获得地图标注对象MKPinAnnotationView,其中采用了可重用MKPinAnnotationView对象设 计。这里使用可重用对象,是为了节约内存。一般情况下,尽可能使用已有对象,减少实例化对象。首先,在第 ②行代码中,我们使用dequeueReusableAnnotationViewWithIdentifier:方法通过一个可重用标识符PIN_ANNOTATION 获得MKPinAnnotationView对象,如果这个对象不存在(第③行代码判断是否存在),则需要使用第④行代码的 initWithAnnotation:reuseIdentifier:构造器创建,其中reuseIdentifier参数是可重用标识符。

第⑤行代码设置大头针标注视图的颜色为紫色。此外,该颜色还可以设置成红色(Swift版MKPinAnnotation- Color.Red,Objective-C版使用MKPinAnnotationColorRed)和绿色(Swift版使用MKPinAnnotationColor.Green, Objective-C版使用MKPinAnnotationColorGreen)。

第⑥行代码说明设置标注视图时,是否以动画效果的形式显示在地图上。第⑦行代码用于在标注点上显示一 些附加信息。如果canShowCallout为true(或YES),则点击“大头针”头时,会出现一个气泡(如图1所示), 而气泡中的文字信息封装在MyAnnotation对象中,其中第一行文字(大一点的文字)保存在title属性中,而第二 行文字(小一点的文字)保存在subtitle属性中。

canShowCallout设置为true的情况

图1 canShowCallout设置为true的情况

在委托方法的后,返回annotationView标注点视图对象。

最后,我们看看自定义标注类MyAnnotation。MyAnnotation的定义如下:

import MapKit

class MyAnnotation: NSObject, MKAnnotation{

//街道信息属性

var streetAddress: String!

//城市信息属性

var city: String!

//州、省、市信息

var state: String!

//邮编

var zip: String!

//地理坐标

var coordinate: CLLocationCoordinate2D

init(coordinate: CLLocationCoordinate2D) {

self.coordinate = coordinate

}

var title: String {

return "您的位置!"

}

var subtitle: String {

var res = NSMutableString()

if (self.state != nil) {

res.appendFormat("%@", self.state)

}

if (self.city != nil) {

res.appendFormat(" • %@", self.state)

}

if (self.zip != nil) {

res.appendFormat(" • %@", self.zip)

}

if (self.streetAddress != nil) {

res.appendFormat(" • %@", self.streetAddress)

}

return res

}

}

//MKAnnotation.h文件

#import

@interface MKAnnotation : NSObject

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

//街道信息属性

@property (nonatomic, copy) NSString *streetAddress;

//城市信息属性

@property (nonatomic, copy) NSString *city;

//州、省、市信息

@property (nonatomic, copy) NSString *state;

//邮编

@property (nonatomic, copy) NSString *zip;

//地理坐标

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

@end

//MKAnnotation.m文件

#import "MKAnnotation.h"

@implementation MKAnnotation

- (NSString *)title {

return @"您的位置!";

}

- (NSString *)subtitle {

NSMutableString *ret = [NSMutableString new];

if (_state)

[ret appendString:_state];

if (_city)

[ret appendString:_city];

if (_city && _state)

[ret appendString:@", "];

if (_streetAddress && (_city || _state || _zip))

[ret appendString:@" • "];

if (_streetAddress)

[ret appendString:_streetAddress];

if (_zip)

[ret appendFormat:@", %@", _zip];

return ret; }

@end

地图上的标注点类必须实现MKAnnotation协议。MKAnnotation协议需要重写如下两个属性。 

1、title:标注点上的主标题。

2、subtitle:标注点上的副标题。

在重写subtitle属性时,我们将它的相关信息拼接成字符串赋值给它。这里,我们可以根据自己的需要和习惯拼接在这个字符串的前后。

好了,关于在南昌APP开发中实现在地图视图上添加标注点的方法就已经介绍完了,如果大家对于第一个步骤:《使用IOS苹果地图添加标注之触发添加动作》的方法还不太理解的话,可点击再重新看一遍,或者来电咨询百恒网络,我们专业为您解答!


400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络小程序

欢迎您的光顾,我们将竭诚为您服务×

售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售后服务 售后服务
 
售后服务 售后服务
 
备案专线 备案专线
 
×