前言
最近在開(kāi)發(fā)國(guó)際版APP時(shí)需要用到谷歌地圖递胧,由于資料比較少求妹,所以這里記錄一下接入過(guò)程和基本的地圖功能的使用温鸽。
1.獲取API key
需要有去墻外進(jìn)入谷歌地圖開(kāi)放平臺(tái):https://cloud.google.com/maps-platform/?hl=zh-CN 這個(gè)是中文語(yǔ)言的地址肥哎,先登錄谷歌賬號(hào)葫笼,然后點(diǎn)擊使用入門屹逛,按照步驟:選擇產(chǎn)品 --->設(shè)置結(jié)算信息(這里需要按照提示綁定國(guó)外的信用卡础废,這一步是必須的,否則無(wú)法使用谷歌地圖) 罕模,做完這兩步后即可啟用谷歌地圖api评腺,然后獲取到apiKey
這是谷歌地圖的文檔地址https://developers.google.com/maps/documentation/ios-sdk/get-api-key
可以看到這句話,這里提示的是上一步設(shè)置結(jié)算信息是必須的淑掌,只有設(shè)置了這個(gè)才能獲取到API key
2.添加API key到APP中
pod導(dǎo)入谷歌地圖蒿讥,我導(dǎo)入了地圖API 和地圖位置API,如果只需要圖層和定位功能抛腕,可能就不需要GooglePlaces
#谷歌地圖
pod'GoogleMaps'
pod'GooglePlaces'
在AppDelegate.m文件中添加以下代碼芋绸,key是相同值
@import GoogleMaps;
@import GooglePlaces;
//配置谷歌地圖
[GMSServices provideAPIKey:@"YOUR_API_KEY"];
[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
3.開(kāi)始使用谷歌地圖API
這里是谷歌地圖的demo,有需要的可以直接下載下來(lái)
https://github.com/googlemaps/maps-sdk-for-ios-samples
1)初始化mapView
//設(shè)置地圖view担敌,這里是隨便初始化了一個(gè)經(jīng)緯度摔敛,在獲取到當(dāng)前用戶位置到時(shí)候會(huì)直接更新的
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
_mapView= [GMSMapViewmapWithFrame:CGRectZerocamera:camera];
_mapView.delegate = self;
_mapView.settings.compassButton = YES;
_mapView.frame = self.view.frame;
[self.view addSubview:_mapView];
2)初始化locationManager
// 1、判斷設(shè)備是否開(kāi)啟定位服務(wù)
if (![CLLocationManager locationServicesEnabled]) {
// 彈框提示
[NSObject mh_showAlertViewWithTitle:@"溫馨提示"message:@"您的設(shè)備暫未開(kāi)啟定位服務(wù)全封!"confirmTitle:@"確定"];
return;
}
// 2舷夺、初始化定位服務(wù)
_locationManager = [[CLLocationManager alloc] init];
// 3苦酱、請(qǐng)求定位授權(quán)*
// 請(qǐng)求在使用期間授權(quán)(彈框提示用戶是否允許在使用期間定位),需添加NSLocationWhenInUseUsageDescription到info.plist
[_locationManager requestWhenInUseAuthorization];
// 請(qǐng)求在后臺(tái)定位授權(quán)(彈框提示用戶是否允許不在使用App時(shí)仍然定位),需添加NSLocationAlwaysUsageDescription添加key到info.plist
[_locationManager requestAlwaysAuthorization];
// 4、設(shè)置定位精度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 5给猾、設(shè)置定位頻率疫萤,每隔多少米定位一次
//_locationManager.distanceFilter = 10.0;
// 6、設(shè)置代理
_locationManager.delegate = self;
// 7敢伸、開(kāi)始定位
// 注意:開(kāi)始定位比較耗電扯饶,不需要定位的時(shí)候最好調(diào)用 [stopUpdatingLocation] 結(jié)束定位。
[_locationManager startUpdatingLocation];
3)CLLocationManagerDelegate
// 位置更新
- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations {
if(!_firstLocationUpdate){
_firstLocationUpdate = YES;//只定位一次的標(biāo)記值
// 獲取最新定位
CLLocation*location = locations.lastObject;
// 打印位置信息
NSLog(@"經(jīng)度:%.2f, 緯度:%.2f", location.coordinate.latitude,location.coordinate.longitude);
// 停止定位
[_locationManager stopUpdatingLocation];
//如果是國(guó)內(nèi)池颈,就會(huì)轉(zhuǎn)化坐標(biāo)系尾序,如果是國(guó)外坐標(biāo),則不會(huì)轉(zhuǎn)換躯砰。
_coordinate2D = [JZLocationConverter wgs84ToGcj02:location.coordinate];
//移動(dòng)地圖中心到當(dāng)前位置
_mapView.camera = [GMSCameraPosition cameraWithTarget:_coordinate2D
zoom:14];
}
}
這里用到了一個(gè)開(kāi)源庫(kù) JZLocationConverter每币,它是用來(lái)處理在國(guó)內(nèi)定位,獲取到的坐標(biāo)系和國(guó)外定位坐標(biāo)系的轉(zhuǎn)化問(wèn)題琢歇,可以查看相關(guān)資料了解有關(guān)定位坐標(biāo)系的知識(shí)兰怠。
https://github.com/JackZhouCn/JZLocationConverter
4) GMSMapViewDelegate
//地圖移動(dòng)后的代理方法,我這里的需求是地圖移動(dòng)需要刷新網(wǎng)絡(luò)請(qǐng)求李茫,查找附近的店鋪
-(void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position{
}
5)GMSAutocompleteViewControllerDelegate
我這里有用到谷歌地圖的位置搜索揭保,它這里是封裝好的VC,可以直接使用魄宏,可以自定義等有很多功能秸侣,具體可以看上面發(fā)過(guò)的谷歌地圖demo地址
在點(diǎn)擊搜索位置按鈕的方法里可以寫如下代碼:
//記得要#import <GooglePlaces/GooglePlaces.h>
GMSAutocompleteViewController*autocompleteViewController =
[[GMSAutocompleteViewController alloc] init];
autocompleteViewController.delegate=self;
[self presentViewController:autocompleteViewController animated:YES completion:nil];
//選擇了位置后的回調(diào)方法
- (void)viewController:(GMSAutocompleteViewController*)viewController
didAutocompleteWithPlace:(GMSPlace*)place {
//移動(dòng)地圖中心到選擇的位置
_mapView.camera = [GMSCameraPosition cameraWithTarget:place.coordinate
zoom:14];
// Dismiss the view controller and tell our superclass to populate the result view.
[viewControllerdismissViewControllerAnimated:YES completion:nil];
}
//失敗回調(diào)
- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error {
// Dismiss the view controller and notify our superclass of the failure.
[viewController dismissViewControllerAnimated:YES completion:nil];
//[self autocompleteDidFail:error];
}
//取消回調(diào)
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
// Dismiss the controller and show a message that it was canceled.
[viewController dismissViewControllerAnimated:YES completion:nil];
//[self autocompleteDidCancel];
}
6)添加marker
-(void)addMarkers{
// Add a custom 'glow' marker around Sydney.
NSArray * latArr = @[@(_coordinate2D.latitude +0.004),@(_coordinate2D.latitude +0.008),@(_coordinate2D.latitude +0.007),@(_coordinate2D.latitude -0.0022),@(_coordinate2D.latitude -0.004)];
NSArray * lngArr = @[@(_coordinate2D.longitude+0.007),@(_coordinate2D.longitude+0.001),@(_coordinate2D.longitude+0.003),@(_coordinate2D.longitude+0.003),@(_coordinate2D.longitude-0.008)];
for(int i =0;i < latArr.count; i++){
GMSMarker*sydneyMarker = [[GMSMarkeralloc]init];
sydneyMarker.title=@"Sydney!";
sydneyMarker.icon= [UIImageimageNamed:@"marker"];
sydneyMarker.position=CLLocationCoordinate2DMake([latArr[i]doubleValue], [lngArr[i]doubleValue]);
sydneyMarker.map=_mapView;
}
}
總結(jié)
本篇主要是介紹了作者接入谷歌地圖的步驟,和實(shí)現(xiàn)一些需求所用到的地圖的部分功能宠互。
谷歌地圖還封裝好了顯示當(dāng)前定位信息的方法味榛,可以直接啟用定位,然后使用kvo監(jiān)聽(tīng)定位成功回調(diào),不過(guò)這里我未找到能讓它暫停定位的方法予跌,為了APP的省電原因搏色,所以采用了系統(tǒng)的CoreLocation來(lái)實(shí)現(xiàn)定位功能
_mapView.settings.myLocationButton = YES;
有其他需求,可參照谷歌地圖demo工程
期待
1.文章若對(duì)您有些許幫助匕得,請(qǐng)給個(gè)喜歡继榆,畢竟碼字不易;若對(duì)您沒(méi)啥幫助汁掠,請(qǐng)給點(diǎn)建議略吨,切記學(xué)無(wú)止境。
2.針對(duì)文章所述內(nèi)容考阱,閱讀期間任何疑問(wèn)翠忠;請(qǐng)?jiān)谖恼碌撞吭u(píng)論指出,我會(huì)火速解決和修正問(wèn)題乞榨。