最近在开发国际版APP时须要用到谷歌地图,因为资料比较少,因此这里记录一下接入过程和基本的地图功能的使用。ios
须要有去墙外进入谷歌地图开放平台:cloud.google.com/maps-platfo… 这个是中文语言的地址,先登陆谷歌帐号,而后点击使用入门,按照步骤:选择产品 --->设置结算信息(这里须要按照提示绑定国外的信用卡,这一步是必须的,不然没法使用谷歌地图) ,作完这两步后便可启用谷歌地图api,而后获取到apiKeygit
这是谷歌地图的文档地址developers.google.com/maps/docume…github
能够看到这句话,这里提示的是上一步设置结算信息是必须的,只有设置了这个才能获取到API keyapi
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"];
复制代码
这里是谷歌地图的demo,有须要的能够直接下载下来ide
//设置地图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];
复制代码
// 一、判断设备是否开启定位服务
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];
复制代码
// 位置更新
- (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
//地图移动后的代理方法,我这里的需求是地图移动须要刷新网络请求,查找附近的店铺
-(void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position{
}
复制代码
我这里有用到谷歌地图的位置搜索,它这里是封装好的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];
}
复制代码
-(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.针对文章所述内容,阅读期间任何疑问;请在文章底部评论指出,我会火速解决和修正问题。