iOS接入谷歌地圖SDK

前言

最近在開發國際版APP時須要用到谷歌地圖,因爲資料比較少,因此這裏記錄一下接入過程和基本的地圖功能的使用。ios

1.獲取API key

須要有去牆外進入谷歌地圖開放平臺:cloud.google.com/maps-platfo… 這個是中文語言的地址,先登陸谷歌帳號,而後點擊使用入門,按照步驟:選擇產品 --->設置結算信息(這裏須要按照提示綁定國外的信用卡,這一步是必須的,不然沒法使用谷歌地圖)  ,作完這兩步後便可啓用谷歌地圖api,而後獲取到apiKeygit

這是谷歌地圖的文檔地址developers.google.com/maps/docume…github

image

能夠看到這句話,這裏提示的是上一步設置結算信息是必須的,只有設置了這個才能獲取到API keyapi

2.添加API key到APP中

pod導入谷歌地圖,我導入了地圖API 和地圖位置API,若是隻須要圖層和定位功能,可能就不須要GooglePlacesbash

#谷歌地圖

    pod'GoogleMaps'

    pod'GooglePlaces'

複製代碼

在AppDelegate.m文件中添加如下代碼,key是相同值網絡

@import GoogleMaps;
@import GooglePlaces;


複製代碼
//配置谷歌地圖

[GMSServices provideAPIKey:@"YOUR_API_KEY"];

[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];

複製代碼

3.開始使用谷歌地圖API

這裏是谷歌地圖的demo,有須要的能夠直接下載下來ide

github.com/googlemaps/…ui

1)初始化mapView

 //設置地圖view,這裏是隨便初始化了一個經緯度,在獲取到當前用戶位置到時候會直接更新的

    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

    // 一、判斷設備是否開啓定位服務

    if (![CLLocationManager locationServicesEnabled]) {

        // 彈框提示

        [NSObject mh_showAlertViewWithTitle:@"舒適提示"message:@"您的設備暫未開啓定位服務!"confirmTitle:@"肯定"];

        return;

    }


    // 二、初始化定位服務

    _locationManager = [[CLLocationManager alloc] init];

    // 三、請求定位受權*

    // 請求在使用期間受權(彈框提示用戶是否容許在使用期間定位),需添加NSLocationWhenInUseUsageDescription到info.plist

    [_locationManager requestWhenInUseAuthorization];

    // 請求在後臺定位受權(彈框提示用戶是否容許不在使用App時仍然定位),需添加NSLocationAlwaysUsageDescription添加key到info.plist

    [_locationManager requestAlwaysAuthorization];

    // 四、設置定位精度

    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // 五、設置定位頻率,每隔多少米定位一次

    //_locationManager.distanceFilter = 10.0;

    // 六、設置代理

    _locationManager.delegate = self;

    // 七、開始定位

    // 注意:開始定位比較耗電,不須要定位的時候最好調用 [stopUpdatingLocation] 結束定位。

    [_locationManager startUpdatingLocation];

複製代碼

3)CLLocationManagerDelegate

// 位置更新

- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations {

    if(!_firstLocationUpdate){

        _firstLocationUpdate = YES;//只定位一次的標記值

        // 獲取最新定位

        CLLocation*location = locations.lastObject;

        // 打印位置信息

        NSLog(@"經度:%.2f, 緯度:%.2f", location.coordinate.latitude,location.coordinate.longitude);

        // 中止定位

        [_locationManager stopUpdatingLocation];

        //若是是國內,就會轉化座標系,若是是國外座標,則不會轉換。

        _coordinate2D = [JZLocationConverter wgs84ToGcj02:location.coordinate];

        //移動地圖中心到當前位置

        _mapView.camera = [GMSCameraPosition cameraWithTarget:_coordinate2D

                                                         zoom:14];

    }

}

複製代碼

這裏用到了一個開源庫 JZLocationConverter,它是用來處理在國內定位,獲取到的座標系和國外定位座標系的轉化問題,能夠查看相關資料瞭解有關定位座標系的知識。google

github.com/JackZhouCn/…spa

4) GMSMapViewDelegate

//地圖移動後的代理方法,我這裏的需求是地圖移動須要刷新網絡請求,查找附近的店鋪

-(void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position{

}

複製代碼

5)GMSAutocompleteViewControllerDelegate

我這裏有用到谷歌地圖的位置搜索,它這裏是封裝好的VC,能夠直接使用,能夠自定義等有不少功能,具體能夠看上面發過的谷歌地圖demo地址

在點擊搜索位置按鈕的方法裏能夠寫以下代碼:

//記得要#import <GooglePlaces/GooglePlaces.h>
    GMSAutocompleteViewController*autocompleteViewController =

    [[GMSAutocompleteViewController alloc] init];

    autocompleteViewController.delegate=self;

    [self presentViewController:autocompleteViewController animated:YES completion:nil];

複製代碼
//選擇了位置後的回調方法

- (void)viewController:(GMSAutocompleteViewController*)viewController

didAutocompleteWithPlace:(GMSPlace*)place {

    //移動地圖中心到選擇的位置

    _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];

}
//失敗回調
- (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];
}
//取消回調
- (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;

    }

}

複製代碼

總結

本篇主要是介紹了做者接入谷歌地圖的步驟,和實現一些需求所用到的地圖的部分功能。

谷歌地圖還封裝好了顯示當前定位信息的方法,能夠直接啓用定位,而後使用kvo監聽定位成功回調,不過這裏我未找到能讓它暫停定位的方法,爲了APP的省電緣由,因此採用了系統的CoreLocation來實現定位功能

_mapView.settings.myLocationButton = YES;

複製代碼

有其餘需求,可參照谷歌地圖demo工程

期待

1.文章若對您有些許幫助,請給個喜歡,畢竟碼字不易;若對您沒啥幫助,請給點建議,切記學無止境。

2.針對文章所述內容,閱讀期間任何疑問;請在文章底部評論指出,我會火速解決和修正問題。

相關文章
相關標籤/搜索