蘋果原生地圖集成基本功能

目前APP端能夠集成的地圖國內(nèi)的有百度飘哨,高德,國外的有谷歌琐凭,但如果做國際化芽隆,對安卓開發(fā)來說就要接入兩種地圖 (由于天朝的規(guī)定,大家都懂得) 但對于蘋果地圖開發(fā)來說 蘋果地圖就能滿足這種需求统屈,話不多說胚吁, 代碼走起!

1.接入蘋果原生地圖首先要導(dǎo)入#import <MapKit/MapKit.h>? 并且實現(xiàn)MKMapViewDelegate,CLLocationManagerDelegate 代理
1.1 創(chuàng)建地圖
- (void)creatMap{

? ? _map = [[MKMapView alloc]initWithFrame:self.bounds];

? ? _map.delegate=self;

? ? _map.userTrackingMode = MKUserTrackingModeFollow;

? ? //顯示指南針

? ? _map.showsCompass = YES;

? ? //顯示比例尺

? ? _map.showsScale = YES;

? ? //顯示交通狀況

? ? _map.showsTraffic = YES;

? ? //顯示建筑物

? ? _map.showsBuildings = YES;

? ? //顯示用戶所在的位置

? ? _map.showsUserLocation = YES;

? ? //顯示感興趣的東西

? ? _map.showsPointsOfInterest = YES;

? ? [self addSubview:_map];





? ? _addressLabel = [[UILabel alloc]init];

? ? _addressLabel.bounds=CGRectMake(0,0,200,40);

? ? _addressLabel.backgroundColor = [UIColor blackColor];

? ? _addressLabel.textColor = [UIColor whiteColor];

? ? _addressLabel.center = self.center;

? ? [self addSubview:_addressLabel];



? ? UITextField*keywordSearchButton = [[UITextFieldalloc]init];

? ? keywordSearchButton.backgroundColor= [UIColorlightGrayColor];

? ? keywordSearchButton.frame=CGRectMake(100,64,100,40);

? ? [keywordSearchButtonaddTarget:self action:@selector(keywordSearch:) forControlEvents:UIControlEventEditingChanged];

? ? [selfaddSubview:keywordSearchButton];


? ? UIButton *hotSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];

? ? hotSearchButton.frame=CGRectMake(0,64,100,40);

? ? [hotSearchButtonsetTitle:@"熱點搜索"forState:UIControlStateNormal];

? ? [hotSearchButtonaddTarget:self action:@selector(hotSeatch) forControlEvents:UIControlEventTouchUpInside];

? ? [selfaddSubview:hotSearchButton];



? ? _placetab = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, self.frame.size.width, 300)];

? ? _placetab.delegate = self;

? ? _placetab.dataSource = self;

? ? _placetab.hidden = YES;

? ? [self addSubview:_placetab];


? ? _geoCoder= [[CLGeocoderalloc]init];

}

1.2 實現(xiàn)代理
//每次調(diào)用愁憔,都會把用戶的最新位置(userLocation參數(shù))傳進(jìn)來

- (void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation{


}

- (void)mapView:(MKMapView*)mapView regionWillChangeAnimated:(BOOL)animated{


? ? //? ? JKAnnotation *anno = [[JKAnnotation alloc] init];

? ? //? ? anno.title = @"我是一個大頭針";

? ? //? ? anno.subtitle = @"我有一個小弟叫小頭";

? ? //? ? anno.coordinate = CLLocationCoordinate2DMake(mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude);

? ? //? ? [mapView addAnnotation:anno];


? ? CLLocation *currLocation = [[CLLocation alloc]initWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude];

? ? [_geoCoderreverseGeocodeLocation:currLocationcompletionHandler:^(NSArray*placemarks,NSError*error) {


? ? ? ? for(CLPlacemark* placemarkinplacemarks) {


? ? ? ? ? ? NSDictionary*address = [placemarkaddressDictionary];

? ? ? ? ? ? _addressLabel.text= [addressobjectForKey:@"Name"];

? ? ? ? ? ? NSLog(@"%@", [addressobjectForKey:@"Name"]);

? ? ? ? }


? ? }];


? ? NSLog(@"------%f",mapView.centerCoordinate.latitude);

}


2.定位功能

如果想單一實現(xiàn)定位功能 系統(tǒng)有一個?CLLocationManager 實現(xiàn)CLLocationManagerDelegate
2.1 開始定位
-(void)startLocation{


? ? if ([CLLocationManager locationServicesEnabled]) {//判斷定位操作是否被允許


? ? ? ? self.locationManager= [[CLLocationManageralloc]init];


? ? ? ? self.locationManager.delegate = self;//遵循代理


? ? ? ? self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;


? ? ? ? self.locationManager.distanceFilter = 10.0f;


? ? ? ? [_locationManager requestWhenInUseAuthorization];//使用程序其間允許訪問位置數(shù)據(jù)(iOS8以上版本定位需要)


? ? ? ? [self.locationManager startUpdatingLocation];//開始定位


? ? }else{//不能定位用戶的位置的情況再次進(jìn)行判斷腕扶,并給與用戶提示


? ? ? ? //1.提醒用戶檢查當(dāng)前的網(wǎng)絡(luò)狀況


? ? ? ? //2.提醒用戶打開定位開關(guān)


? ? }


}

//實現(xiàn)代理
- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{


? ? //當(dāng)前所在城市的坐標(biāo)值

? ? CLLocation*currLocation = [locationslastObject];

? ? NSLog(@"經(jīng)度=%f 緯度=%f 高度=%f", currLocation.coordinate.latitude,currLocation.coordinate.longitude,currLocation.altitude);


? ? //根據(jù)經(jīng)緯度反向地理編譯出地址信息

? ? CLGeocoder* geoCoder = [[CLGeocoderalloc]init];


? ? [geoCoderreverseGeocodeLocation:currLocationcompletionHandler:^(NSArray*placemarks,NSError*error) {


? ? ? ? for(CLPlacemark* placemarkinplacemarks) {


? ? ? ? ? ? NSDictionary*address = [placemarkaddressDictionary];


? ? ? ? ? ? //? Country(國家)? State(省)? City(市)

? ? ? ? ? ? NSLog(@"#####%@",address);


? ? ? ? ? ? NSLog(@"%@", [addressobjectForKey:@"Country"]);


? ? ? ? ? ? NSLog(@"%@", [addressobjectForKey:@"State"]);


? ? ? ? ? ? NSLog(@"%@", [addressobjectForKey:@"City"]);



? ? ? ? }


? ? }];


}


-(void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error{


? ? if([errorcode] ==kCLErrorDenied){

? ? ? ? //訪問被拒絕

? ? }

? ? if ([error code] == kCLErrorLocationUnknown) {

? ? ? ? //無法獲取位置信息

? ? }

}

3.搜索功能
//關(guān)鍵字搜索

- (void)keywordSearch:(UITextField*)field {

? ? if(field.text.length==0){

? ? ? ? _placetab.hidden=YES;

? ? }

? ? //創(chuàng)建地理編碼

? ? CLGeocoder*geocoder = [[CLGeocoderalloc]init];

? ? //正向地理編碼

? ? [geocodergeocodeAddressString:field.textcompletionHandler:^(NSArray*_Nullableplacemarks,NSError*_Nullableerror) {


? ? ? ? if(error ==nil) {

? ? ? ? ? ? //解析地理位置成功

? ? ? ? ? ? //成功后遍歷數(shù)組

? ? ? ? ? ? for(CLPlacemark*placeinplacemarks) {


? ? ? ? ? ? ? ? //創(chuàng)建大頭針


? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MyPointAnnotation *annotation = [[MyPointAnnotation alloc] initWithCoorDinate:place.location.coordinate title:place.name subTitle:place.locality information:place.locality];

? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將大頭針加入到地圖

? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [_map addAnnotation:annotation];

? ? ? ? ? ? ? ? [self.dataArrayremoveAllObjects];

? ? ? ? ? ? ? ? [self.dataArrayaddObject:place];

? ? ? ? ? ? ? ? _placetab.hidden=NO;

? ? ? ? ? ? ? ? [_placetabreloadData];





? ? ? ? ? ? }


? ? ? ? }else{


? ? ? ? ? ? NSLog(@"正向地理編碼解析失敗");

? ? ? ? }


? ? }];


}
//熱門搜索

- (void)hotSeatch {

? ? //創(chuàng)建本地搜索請求

? ? MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];

? ? //設(shè)置搜索熱點詞(自然語言)

? ? request.naturalLanguageQuery = @"學(xué)校";

? ? //設(shè)置搜索范圍,以某個原點為中心吨掌,向外擴(kuò)展一段經(jīng)緯度距離范圍

? ? CLLocationCoordinate2D origionpoint = CLLocationCoordinate2DMake(36.08397, 120.37126);

? ? //設(shè)置經(jīng)緯度跨越范圍

? ? MKCoordinateSpan span = MKCoordinateSpanMake(0.3, 0.3);

? ? //設(shè)置經(jīng)緯度搜索區(qū)域

? ? MKCoordinateRegionregion =MKCoordinateRegionMake(origionpoint, span);

? ? //將區(qū)域賦值給搜索請求對象中的region屬性中

? ? request.region= region;

? ? //將地圖移動到該區(qū)域

? ? [_mapsetRegion:region];


? ? //創(chuàng)建本地搜索對象

? ? MKLocalSearch*search = [[MKLocalSearchalloc]initWithRequest:request];

? ? //開啟搜索

? ? [searchstartWithCompletionHandler:^(MKLocalSearchResponse*_Nullableresponse,NSError*_Nullableerror) {


? ? ? ? if(error ==nil) {


? ? ? ? ? ? //搜索成功

? ? ? ? ? ? //獲取搜索結(jié)果

? ? ? ? ? ? NSArray*arrResult = response.mapItems;


? ? ? ? ? ? for(MKMapItem*iteminarrResult) {


? ? ? ? ? ? ? ? //先取出地圖目的坐標(biāo)對象(標(biāo)記)

? ? ? ? ? ? ? ? MKPlacemark*placeMark = item.placemark;

? ? ? ? ? ? ? ? /*

?? ? ? ? ? ? ? ? 96? ? ? ? ? ? ? ? ? 地標(biāo)里存放的經(jīng)緯度半抱,以及位置的地理信息說明,如名字思犁、街道等等

?? ? ? ? ? ? ? ? 97? ? ? ? ? ? ? ? ? */

? ? ? ? ? ? ? ? //創(chuàng)建大頭針

? ? ? ? ? ? ? ? JKAnnotation*anno = [[JKAnnotationalloc]init];

? ? ? ? ? ? ? ? anno.title=@"我是一個大頭針";

? ? ? ? ? ? ? ? anno.subtitle=@"我有一個小弟叫小頭";

? ? ? ? ? ? ? ? anno.coordinate= placeMark.location.coordinate;

? ? ? ? ? ? ? ? [_mapaddAnnotation:anno];


? ? ? ? ? ? }



? ? ? ? }else{

? ? ? ? ? ? NSLog(@"搜索失敗");


? ? ? ? }


? ? }];


}
4.自定義大頭針 JKAnnotation
.h

#import <Foundation/Foundation.h>

#import<MapKit/MapKit.h>

@interface JKAnnotation : NSObject<MKAnnotation>

@property (nonatomic) CLLocationCoordinate2D coordinate;

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

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

@end

.m
#import "JKAnnotation.h"

@implementation JKAnnotation

@end


// 已經(jīng)添加了大頭針模型,還沒有完全渲染出來之前(mapView的代理)

- (void)mapView:(MKMapView*)mapView didAddAnnotationViews:(NSArray *)views{

? ? for(MKAnnotationView*annotationViewinviews) {

? ? ? ? //目標(biāo)位置

? ? ? ? CGRecttargetRect = annotationView.frame;

? ? ? ? //先讓其在最頂上

? ? ? ? annotationView.frame=CGRectMake(targetRect.origin.x,0, targetRect.size.width, targetRect.size.height);

? ? ? ? //最后通過動畫展示到最終的目標(biāo)地方

? ? ? ? [UIView animateWithDuration:0.3 animations:^{

? ? ? ? ? ? annotationView.frame= targetRect;

? ? ? ? }];

? ? }

}//如果不要這種效果這段代碼也可以不需要

// 大頭針視圖的重用,大頭針也存在著重用的機(jī)制,方便優(yōu)化內(nèi)存

// 每次添加大頭針都會調(diào)用此方法? 可以設(shè)置大頭針的樣式

- (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation{

? ? // 判斷大頭針位置是否在原點,如果是則不加大頭針或者添加屬于自己特殊圖標(biāo)

? ? if([annotationisKindOfClass:[MKUserLocationclass]]) {returnnil; }

? ? //1.定義一個可重用標(biāo)識符

? ? staticNSString*reuseIdentifier =@"mapView";

? ? MKAnnotationView*annotationView = (MKAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];

? ? if(annotationView ==nil) {

? ? ? ? annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:reuseIdentifier];

? ? }


? ? //設(shè)置可重用標(biāo)識符的相關(guān)屬性

? ? // 顯示標(biāo)題和副標(biāo)題

? ? annotationView.canShowCallout=YES;

? ? // 設(shè)置圖片(用戶頭像,或者商品/超市/汽車/單車等等圖片)

? ? annotationView.image= [UIImageimageNamed:@"header_new"];

? ? //須導(dǎo)入#import "UIImageView+WebCache.h"頭文件

? ? // [annotationView.image sd_setImageWithURL:[NSURL URLWithString:[dict valueForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"默認(rèn)圖片"]];



? ? returnannotationView;


? ? // 判斷大頭針位置是否在原點,如果是則不加大頭針

? ? //? ? if([annotation isKindOfClass:[mapView.userLocation class]]){

? ? //? ? ? ? return nil;

? ? //? ? }

? ? //? ? //設(shè)置自定義大頭針

? ? //? ? JKAnnotationView *annotationView = (JKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"otherAnnotationView"];

? ? //? ? if (annotationView == nil) {

? ? //? ? ? ? annotationView = [[JKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"otherAnnotationView"];

? ? //? ? }

? ? //? ? annotationView.image = [UIImage imageNamed:@"header_new"];


? ? //? return annotationView;

}
demo 地址:https://github.com/XUDAQUAN/AppleMap

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末代虾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子棉磨,更是在濱河造成了極大的恐慌,老刑警劉巖学辱,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乘瓤,死亡現(xiàn)場離奇詭異,居然都是意外死亡策泣,警方通過查閱死者的電腦和手機(jī)衙傀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萨咕,“玉大人统抬,你說我怎么就攤上這事。” “怎么了聪建?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵钙畔,是天一觀的道長。 經(jīng)常有香客問我金麸,道長擎析,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任挥下,我火速辦了婚禮揍魂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘棚瘟。我一直安慰自己现斋,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布解取。 她就那樣靜靜地躺著步责,像睡著了一般。 火紅的嫁衣襯著肌膚如雪禀苦。 梳的紋絲不亂的頭發(fā)上蔓肯,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天,我揣著相機(jī)與錄音振乏,去河邊找鬼蔗包。 笑死,一個胖子當(dāng)著我的面吹牛慧邮,可吹牛的內(nèi)容都是我干的调限。 我是一名探鬼主播,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼误澳,長吁一口氣:“原來是場噩夢啊……” “哼耻矮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起忆谓,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤裆装,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后倡缠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哨免,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年昙沦,在試婚紗的時候發(fā)現(xiàn)自己被綠了琢唾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡盾饮,死狀恐怖采桃,靈堂內(nèi)的尸體忽然破棺而出懒熙,到底是詐尸還是另有隱情,我是刑警寧澤芍碧,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布煌珊,位于F島的核電站,受9級特大地震影響泌豆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜吏饿,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一踪危、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧猪落,春花似錦贞远、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至官疲,卻和暖如春袱结,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背途凫。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工垢夹, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人维费。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓果元,卻偏偏與公主長得像,于是被迫代替她去往敵國和親犀盟。 傳聞我的和親對象是個殘疾皇子而晒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容