欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

iOS 访问 Google 地图 SDK

最编程 2024-03-23 17:15:03
...

前言

最近在开发国际版APP时需要用到谷歌地图,由于资料比较少,所以这里记录一下接入过程和基本的地图功能的使用。

1.获取API key

需要有去墙外进入谷歌地图开放平台:cloud.google.com/maps-platfo… 这个是中文语言的地址,先登录谷歌账号,然后点击使用入门,按照步骤:选择产品 --->设置结算信息(这里需要按照提示绑定国外的信用卡,这一步是必须的,否则无法使用谷歌地图)  ,做完这两步后即可启用谷歌地图api,然后获取到apiKey

这是谷歌地图的文档地址developers.google.com/maps/docume…

image

可以看到这句话,这里提示的是上一步设置结算信息是必须的,只有设置了这个才能获取到API key

2.添加API key到APP中

pod导入谷歌地图,我导入了地图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.开始使用谷歌地图API

这里是谷歌地图的demo,有需要的可以直接下载下来

github.com/googlemaps/…

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


    // 1、判断设备是否开启定位服务

    if (![CLLocationManager locationServicesEnabled]) {

        // 弹框提示

        [NSObject mh_showAlertViewWithTitle:@"温馨提示"message:@"您的设备暂未开启定位服务!"confirmTitle:@"确定"];

        return;

    }


    // 2、初始化定位服务

    _locationManager = [[CLLocationManager alloc] init];

    // 3、请求定位授权*

    // 请求在使用期间授权(弹框提示用户是否允许在使用期间定位),需添加NSLocationWhenInUseUsageDescription到info.plist

    [_locationManager requestWhenInUseAuthorization];

    // 请求在后台定位授权(弹框提示用户是否允许不在使用App时仍然定位),需添加NSLocationAlwaysUsageDescription添加key到info.plist

    [_locationManager requestAlwaysAuthorization];

    // 4、设置定位精度

    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // 5、设置定位频率,每隔多少米定位一次

    //_locationManager.distanceFilter = 10.0;

    // 6、设置代理

    _locationManager.delegate = self;

    // 7、开始定位

    // 注意:开始定位比较耗电,不需要定位的时候最好调用 [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,它是用来处理在国内定位,获取到的坐标系和国外定位坐标系的转化问题,可以查看相关资料了解有关定位坐标系的知识。

github.com/JackZhouCn/…

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.针对文章所述内容,阅读期间任何疑问;请在文章底部评论指出,我会火速解决和修正问题。