麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

詳解iOS集成GoogleMap(定位、搜索)

2019-10-21 18:41:49
字體:
來源:轉載
供稿:網友

簡介:

最近花了些時間看了GoogleMap官方文件并集成到國際版app中,網上關于GoogleMap for iOS的講解相對Android來說少一點,比較有幫助的幾乎全是英文文檔。下面是我開發過程中遇到的坑、以及采用的解決方法。

集成GoogleMap步驟:

  1. 1、Cocoapods導入pod 'GoogleMaps'
  2. 2、獲取API密匙(前提是已經在GoogleMapSDK中創建好自己的應用)
  3. 3、配置plist文件搭建定位環境
  4. 4、調用代理方法實現需求

tips:pod 'GoogleMaps'、pod 'GooglePlaces'、pod 'GooglePlacePicker'這三個框架。(GoogleMaps:顯示基本的定位功能;GooglePlaces:實現搜索功能,官方文檔叫做地點自動完成;GooglePlacePicker:是實現獲取某個POI的的詳細信息,比如名字、詳細地址、路線等)

景點(POI)包括公園、學校和政府大樓,等等。 另外,如果地圖類型為 kGMSTypeNormal,商家景點默認將顯示在地圖上。 商家景點表示商店、餐館和酒店之類的商家。

按照 Google Places API 中的定義,一個 POI 對應于一個地點。 例如,休閑公園為景點,但噴泉之類的地點通常不屬于景點(除非它們具有國家或歷史意義)。

配置plist文件:

打開plist的代碼源文件,輸入:

iOS,GoogleMap,定位,搜索

定位:

一、在AppDelegate 頭文件 導入框架

#import

二、向您的 application:didFinishLaunchingWithOptions: 方法添加以下內容,使用我們剛才獲取到的 API 密鑰替代 YOUR_API_KEY:

[GMSServices provideAPIKey:@"YOUR_API_KEY"];

tips:這一步是在啟動app的時候,GoogleMap準備代理工作。

三、在我們需要顯示地圖的控制器調用API方法

@property (nonatomic,strong) CLLocationManager *locationManager;//地圖定位對象@property (nonatomic,strong) GMSMapView *mapView;//地圖@property (nonatomic,strong) GMSMarker *marker;//大頭針@property (nonatomic,strong) GMSPlacesClient * placesClient;//可以獲取某個地方的信息//注冊的代理@interface TestMapViewController ()

tips:這是在控制器.h文件聲明的屬性。

(一)初始化一個地圖對象

GMSMapView:是控制地圖的外觀類

GMSCameraPosition:是控制地圖要顯示的內容類

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-23.12960481 longitude:113.30887721            zoom:Level]; self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; self.mapView.delegate = self; //注冊代理屬性 self.mapView.settings.compassButton = YES;//顯示指南針 [self.view addSubview:self.mapView];

tips:上面的經緯度可以隨便傳一個,之后會獲取到新的經緯度并更新位置

(二)初始化一個定位管理者對象

if (self.locationManager == nil) {  self.locationManager = [[CLLocationManager alloc]init]; } self.locationManager.delegate = self; [self.locationManager requestAlwaysAuthorization];//授權方式,如果在后臺也需要定位,那就選擇 requestAlwaysAuthorization。 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;//最精確的定位 self.locationManager.distanceFilter = kCLDistanceFilterNone; // 默認是kCLDistanceFilterNone,也可以設置其他值,表示用戶移動的距離小于該范圍內就不會接收到通知 [self.locationManager startUpdatingLocation];

tips:CLLocationManager 是負責獲取用戶行為的類,列如獲取用戶當前位置信息。更多詳細信息請閱覽CLLocationManager。里面講解CLLocationManager的一些應用場景并有代碼實例。

運行app:這時候我們會看到并沒有實景地圖出來,原因是:前面提到的GMSCameraPosition類,我們并沒有在定位成功之后將定位內容賦它。

GMSCameraPosition類,它是負責顯示定位內容的。很重要!

(三)在定位成功的API代理方法中,獲取經緯度并轉成影像賦值

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *curLocation = [locations lastObject]; // 通過location 或得到當前位置的經緯度 CLLocationCoordinate2D curCoordinate2D = curLocation.coordinate; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:curCoordinate2D.latitude longitude:curCoordinate2D.longitude zoom:Level]; CLLocationCoordinate2D position2D = CLLocationCoordinate2DMake(curLocation.coordinate.latitude, curLocation.coordinate.longitude); self.mapView.camera = camera;//這句話很重要很重要,將我們獲取到的經緯度轉成影像并賦值給地圖的camera屬性 [self.locationManager stopUpdatingLocation];//定位成功后停止定位}

tips:locationManager: didUpdateLocations: 代理方法是GoogleMap 中實現定位成功后回調的代理方法,你可以在這里獲取到經緯度。

運行app:這時候地圖就出來了

iOS,GoogleMap,定位,搜索

添加大頭針

GMSMarker類是負責顯示大頭針,默認是紅色,你可以自定義大頭針,用圖片或者改變顏色,具體看官方文檔GMSMarker。

 self.marker = [GMSMarker markerWithPosition:position2D]; self.marker.map = self.mapView;

tips:position2D是在定位成功之后轉換得到的CLLocationCoordinate2D屬性經緯度值。

小坑提示:這時候有可能會出現,定位成功之后出現多個大頭針。原因是:進行定位的時候,map獲取多個預測位置,從而產生生成多個大頭針的現象。解決辦法:在每次生成大頭針之前先清除之前的那個,只生成最精準的最后一個。

[self.marker.map clear]; self.marker.map = nil;

反編碼(經緯度轉成具體位置):

 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; //反地理編碼 [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {     if (error) {  }else{       CLPlacemark *placemark = [placemarks objectAtIndex:0];//第一個位置是最精確的    //賦值詳細地址    DLog(@"placemark---路號name:%@-市locality:%@-區subLocality:%@-省administrativeArea:%@-路thoroughfare:%@",placemark.name,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.thoroughfare);     }];

這時候就已經可以獲取到具體的國家、省、市、區、街道了。

補充:反編碼是獲取不到POI位置的(我獲取不到)。這時候可以使用

self.placesClient = [GMSPlacesClient sharedClient];//獲取某個地點的具體信息 [self.placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {  if (error != nil) {   DLog(@"Current Place error %@", [error localizedDescription]);   return;  }   //  for (GMSPlaceLikelihood *likelihood in likelihoodList.likelihoods) {//   GMSPlace* place = likelihood.place;//   NSLog(@"Current Place name %@ at likelihood %g", place.name, likelihood.likelihood);//   NSLog(@"Current Place address %@", place.formattedAddress);//   NSLog(@"Current Place attributions %@", place.attributions);//   NSLog(@"Current PlaceID %@", place.placeID);//  }   //這里就可以獲取到POI的名字了   //這里做一些你想做的事    }];

點擊地圖并移動大頭針

這里是用到GMSMapViewDelegate的代理回調

回調1:這里是點擊地圖上的某個點API返回的代理方法,在這個代理方法,你可以獲取經緯度去反編譯地址

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{ //點擊一次先清除上一次的大頭針 [self.marker.map clear]; self.marker.map = nil; // 通過location 或得到當前位置的經緯度 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:coordinate.latitude longitude:coordinate.longitude zoom:Level]; CLLocationCoordinate2D position2D = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude); self.mapView.camera = camera; //大頭針 self.marker = [GMSMarker markerWithPosition:position2D]; self.marker.map = self.mapView; CLLocation *curLocation = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];   CLGeocoder *geocoder = [[CLGeocoder alloc]init]; //反地理編碼 [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {     if (error) {       DLog(@"error.description:%@",error.description);      }else{       CLPlacemark *placemark = [placemarks objectAtIndex:0];       //賦值詳細地址   DLog(@"placemark---路號name:%@-市locality:%@-區subLocality:%@-省administrativeArea:%@-路thoroughfare:%@",placemark.name,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.thoroughfare);  }]; }

回調2:這里也是點擊地圖上的某個點API返回的代理方法

- (void)mapView:(GMSMapView *)mapViewdidTapPOIWithPlaceID:(NSString *)placeID   name:(NSString *)name  location:(CLLocationCoordinate2D)location{}

tips:值得注意的,兩者的區別是:第二個點擊代理方法是當你點擊POI的時候才會回調,會返回place的name、ID、經緯度;第一個代理方法是只要點擊地圖任意一個位置就會回調,只會返回經緯度。也就是:每一次的點擊,只會執行其中一個代理方法。

搜索:

搜索功能在官方文檔是叫做“自動完成”,即你輸入一部分的文本,GoogleMap會根據你的文本預測出地點并自動填充返回,具體請看官方文檔自動完成

效果如圖:

iOS,GoogleMap,定位,搜索

這里你需要做的步驟跟做“定位”的一樣:

(1)獲取APIKEY

(2) 在application:didFinishLaunchingWithOptions: 注冊密匙

1[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];

(3) 創建搜索UI并調用代理方法獲取API自動填充的結果數組集

小坑提示: GMSPlacesClient跟GMSServices的密匙是不一樣的,密匙不對的話,會出現反復調用

viewController:didFailAutocompleteWithError:的現象。

tips:搭建搜索UI又幾種方式:1)搜索框直接創建在導航欄 2)搜索欄創建在視圖頂部 3)自定義。根據你的需求用代碼~

(一)這里是第一種方式(搜索框直接創建在導航欄):

GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; acController.delegate = self; [self presentViewController:acController animated:YES completion:nil];

tips:這里就可以直接往搜索框編輯文字,API會直接給你返回搜索結果集合

(二)調用API代理方法:

// Handle the user's selection. 這是用戶選擇搜索中的某個地址后返回的結果回調方法- (void)viewController:(GMSAutocompleteViewController *)viewControllerdidAutocompleteWithPlace:(GMSPlace *)place {   [self dismissViewControllerAnimated:YES completion:nil]; [self.marker.map clear]; self.marker.map = nil; // 通過location 或得到當前位置的經緯度 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:place.coordinate.latitude longitude:place.coordinate.longitude zoom:Level]; CLLocationCoordinate2D position2D = CLLocationCoordinate2DMake(place.coordinate.latitude,place.coordinate.longitude); self.marker = [GMSMarker markerWithPosition:position2D]; self.mapView.camera = camera; self.marker.map = self.mapView;   self.locationLabel.text = place.name; self.locationDetailLabel.text = place.formattedAddress;  }

tips:這個代理方法實現的是,當用戶在搜索集中選擇了在某一個結果返回地圖,并定位添加大頭針。

自動填充失敗的回調:

- (void)viewController:(GMSAutocompleteViewController *)viewControllerdidFailAutocompleteWithError:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; // TODO: handle the error. DLog(@"Error: %@", [error description]);}

tips:自動填充失敗后你可以在這里做一些事,默認是不管的。

補充:搜索欄的外觀是可以自定義的,你可以設置成跟自己的app一樣的風格~具體請看設置 UI 控件樣式屬性

到這里,搜索功能就算完成了。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产一区网址 | 国产美女视频免费 | 国产成人在线综合 | xvideos korean | 久久96国产精品久久久 | 亚洲精品7777| 末成年女av片一区二区 | 欧美日韩国产综合网 | 国产精品久久久久久久久久 | 粉嫩蜜桃麻豆免费大片 | 九草av| 久久亚洲精品11p | 国产一级小视频 | 日韩视频在线观看免费视频 | 黄视频在线网站 | 深夜精品福利 | 亚洲精品自在在线观看 | 欧美一级免费在线观看 | 中文字幕h | 日本不卡一区二区三区在线观看 | 日韩电影一区二区 | 黄视频在线网站 | 国产视频在线免费观看 | 欧美一级毛片免费观看 | 国产精品视频二区不卡 | 国产成人av免费 | 性欧美xxxx极品摘花 | 黄色影院在线观看视频 | 久久伊人精品视频 | 久久新网址 | 色999久久久精品人人澡69 | 亚洲国产高清视频 | www.国产免费 | av手机免费在线观看 | 最新中文字幕日本 | 91精品国产综合久久婷婷香蕉 | 久久久久免费精品国产小说色大师 | 久久久久久久久久亚洲精品 | 国产精品久久久久久久久粉嫩 | 久久久久久久高清 | 全免费午夜一级毛片真人 |