欧美亚洲大片-国产免费成人在线视频-国产成人8x人在线视频软件-日韩亚洲欧美中文字幕-97伦伦午夜电影理伦片-亚洲综合精品视频-曰本女人牲交高潮视频-久久久精品欧美-天天干天天操天天碰-精品国产乱码久久久久久1区2区-黄色一级免费片-在线免费欧美-国产成人精品在线视频-精人妻一区二区三区-香蕉视频2020-综合久久99-欧美日韩亚洲国产综合

十年專注于品牌網(wǎng)站建設(shè) 十余年專注于網(wǎng)站建設(shè)_小程序開發(fā)_APP開發(fā),低調(diào)、敢創(chuàng)新、有情懷!
南昌百恒網(wǎng)絡(luò)微信公眾號 掃一掃關(guān)注
小程序
tel-icon全國服務(wù)熱線:400-680-9298,0791-88117053
掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號
掃一掃打開百恒網(wǎng)絡(luò)微信小程序

百恒網(wǎng)絡(luò)

南昌百恒網(wǎng)絡(luò)

實現(xiàn)地圖委托方法mapView:viewForAnnotation:

百恒網(wǎng)絡(luò) 2017-09-19 6274

昨天南昌APP制作開發(fā)公司-百恒網(wǎng)絡(luò)為大家介紹了使用IOS蘋果地圖添加標注的第一個步驟:觸發(fā)添加動作,那么今天本公司就來為大家介紹第二步:實現(xiàn)地圖委托方法 mapView:viewForAnnotation:,這也是最后一步!具體方法如下:

MKMapViewDelegate委托協(xié)議方法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:在地圖視圖添加標注時回調(diào)。給地圖視圖添加標注的方法是self.mapView.addAnnotation(annotation),其中annotation是地圖標注對象。

第②~④行代碼用于獲得地圖標注對象MKPinAnnotationView,其中采用了可重用MKPinAnnotationView對象設(shè) 計。這里使用可重用對象,是為了節(jié)約內(nèi)存。一般情況下,盡可能使用已有對象,減少實例化對象。首先,在第 ②行代碼中,我們使用dequeueReusableAnnotationViewWithIdentifier:方法通過一個可重用標識符PIN_ANNOTATION 獲得MKPinAnnotationView對象,如果這個對象不存在(第③行代碼判斷是否存在),則需要使用第④行代碼的 initWithAnnotation:reuseIdentifier:構(gòu)造器創(chuàng)建,其中reuseIdentifier參數(shù)是可重用標識符。

第⑤行代碼設(shè)置大頭針標注視圖的顏色為紫色。此外,該顏色還可以設(shè)置成紅色(Swift版MKPinAnnotation- Color.Red,Objective-C版使用MKPinAnnotationColorRed)和綠色(Swift版使用MKPinAnnotationColor.Green, Objective-C版使用MKPinAnnotationColorGreen)。

第⑥行代碼說明設(shè)置標注視圖時,是否以動畫效果的形式顯示在地圖上。第⑦行代碼用于在標注點上顯示一 些附加信息。如果canShowCallout為true(或YES),則點擊“大頭針”頭時,會出現(xiàn)一個氣泡(如圖1所示), 而氣泡中的文字信息封裝在MyAnnotation對象中,其中第一行文字(大一點的文字)保存在title屬性中,而第二 行文字(小一點的文字)保存在subtitle屬性中。

canShowCallout設(shè)置為true的情況

圖1 canShowCallout設(shè)置為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

地圖上的標注點類必須實現(xiàn)MKAnnotation協(xié)議。MKAnnotation協(xié)議需要重寫如下兩個屬性。 ?

1、title:標注點上的主標題。

2、subtitle:標注點上的副標題。

在重寫subtitle屬性時,我們將它的相關(guān)信息拼接成字符串賦值給它。這里,我們可以根據(jù)自己的需要和習慣拼接在這個字符串的前后。

好了,關(guān)于在南昌APP開發(fā)中實現(xiàn)在地圖視圖上添加標注點的方法就已經(jīng)介紹完了,如果大家對于第一個步驟:《使用IOS蘋果地圖添加標注之觸發(fā)添加動作》的方法還不太理解的話,可點擊再重新看一遍,或者來電咨詢百恒網(wǎng)絡(luò),我們專業(yè)為您解答!


400-680-9298,0791-88117053
掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號
掃一掃打開百恒網(wǎng)絡(luò)小程序

歡迎您的光顧,我們將竭誠為您服務(wù)×

售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售后服務(wù) 售后服務(wù)
 
售后服務(wù) 售后服務(wù)
 
備案專線 備案專線
 
×
岚皋县| 连城县| 抚州市| 平阳县| 万荣县| 金阳县| 阿拉善盟| 镇远县| 永嘉县| 阳东县| 元氏县| 泗水县| 安多县| 梅河口市| 乐至县| 南投县| 甘南县| 汉源县| 宁阳县| 银川市| 大理市| 丰台区| 宣恩县| 琼海市| 绵竹市| 古浪县| 新河县| 廊坊市| 威海市| 洞口县| 遵义县| 罗甸县| 千阳县| 德清县| 桦甸市| 北票市| 通化县| 三河市| 鄯善县| 临泽县| 南江县|