目前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