谷歌地图简单使用-iOS

官方集成文档 :

谷歌API请求地址谷歌链接地址
Google Maps API谷歌地图API文档
Google Places API谷歌地图API文档

1、您需要在谷歌平台注册开发者,创建项目获取到appKey,

2、Pod地图

pod'GoogleMaps' pod'GooglePlaces'

3、在appDelegate引入头文件

@import GoogleMaps;
@import GooglePlaces;

4、配置key

  //配置谷歌地图
    [GMSServices provideAPIKey:@"您在谷歌创建项目的appkey"];
    [GMSPlacesClient provideAPIKey:@"您在谷歌创建项目的appkey"];

5、在需要用到地图的文件引入头文件并设置代理

#import <GoogleMaps/GoogleMaps.h> <GMSMapViewDelegate:代理>

//全局方法
@property (nonatomic, strong)CLLocationManager*loacationManager;

GMSMapView* mapView;
CLLocationCoordinate2D coordinate2D;

6、 地图初始化

-(void)createMapView{
    mapView = [[GMSMapView alloc] initWithFrame:CGRectMake(0, 0, JXYSWDScreenWidth, JXYSWDScreenHeight)];
    mapView.delegate = self;
    mapView.indoorEnabled = NO;
    mapView.settings.rotateGestures = NO;
    mapView.settings.tiltGestures = NO;
    mapView.settings.myLocationButton = YES;
    mapView.myLocationEnabled = YES;
    self.loacationManager = [[CLLocationManager alloc] init];
    self.loacationManager.delegate  = self;
    [self.loacationManager requestWhenInUseAuthorization];
    [self.view addSubview:mapView];
}

7、获取当前用户的位置,然后在地图显示当前用户的位置

初始化定位

-(void)getCurrentUserLocation{
//    // 初始化定位管理器
   self.loacationManager = [[CLLocationManager alloc] init];
   // 设置代理
   self.loacationManager.delegate = self;
   // 设置定位精确度到米
   self.loacationManager.desiredAccuracy = kCLLocationAccuracyBest;
   // 设置过滤器为无
   self.loacationManager.distanceFilter = kCLDistanceFilterNone;
   // 取得定位权限,有两个方法,取决于你的定位使用情况
   // 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
   // 这句话ios8以上版本使用。
   [self.loacationManager requestWhenInUseAuthorization];
   // 开始定位
   [self.loacationManager startUpdatingLocation];
}

获取定位授权状态

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status)
    {
        case kCLAuthorizationStatusDenied:                  // 拒绝授权
            NSLog(@"授权失败:用户拒绝授权或未开启定位服务");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:     // 在使用期间使用定位
            NSLog(@"授权成功:用户允许应用“使用期间”使用定位服务");
             [self.loacationManager startUpdatingLocation];
            break;
    }
}

#pragma mark - CLLocationManagerDelegate

位置更新时调用,谷歌地图在中国的位置上是不准确的(这里有一个第三方库,因为谷歌在国内使用的话坐标需要转换,国外就不需要,这边使用的是JZLocationConverter这个第三方库,直接继承进去就好- pod ‘JZLocationConverter’

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations  {
    /**
     * 位置更新时调用
     */
    CLLocation*location = locations.lastObject;
    mapView.camera = [[GMSCameraPosition alloc] initWithTarget:location.coordinate zoom:15 bearing:0 viewingAngle:0];
    [_loacationManager stopUpdatingLocation];
    //获取当前坐标名
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks && [placemarks count] > 0) {
            // 纬度
            [JXYSWDUserData shareManager].latitudeFloat = location.coordinate.latitude;
            // 经度
            [JXYSWDUserData shareManager].longitudeFloat = location.coordinate.longitude;
            //如果是国内,就会转化坐标系,如果是国外坐标,则不会转换。
            coordinate2D = [JZLocationConverter wgs84ToGcj02:location.coordinate];
            //移动地图中心到当前位置
            mapView.camera = [GMSCameraPosition cameraWithTarget:coordinate2D zoom:14];
            [self deviceSearchRequest:@{@"current":@"1",@"longitude":@([JXYSWDUserData shareManager].longitudeFloat),@"latitude":@([JXYSWDUserData shareManager].latitudeFloat)}];
            NSString *cityName = @"未知";
            for (CLPlacemark *place in placemarks) {
                if (place.name.length > 0) {
                    cityName = place.name;
    //                        NSLog(@"%@ ---- %@",cityName,place.locality);//城市

                }
            }
        }
    }];
    // 停止定位
    [self.loacationManager stopUpdatingLocation];
}

8、获取用户位置信息记得需要在info授权和设置代理

a、代理CLLocationManagerDelegate

b、info授权(好像有一个是一直获取用户授权的权限,具体百度看一下)

Privacy - Location Always and When In Use Usage Description;
Privacy - Location Always Usage Description;
Privacy - Location When In Use Usage Description;

9、假如需要在地图显示图标的话,使用以下的方法

在地图上添加图标

-(void)addMarkers{
    for(int i =0;i < array.count; i++){
           CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
        GMSMarker *sydneyMarker = [GMSMarker markerWithPosition:position];
        sydneyMarker.title=@"点击的时候显示标题";
        sydneyMarker.icon = [GMSMarker markerImageWithColor:[UIColor blackColor]];
        sydneyMarker.icon=@"这个是自定义坐标图片";
        //这个是核心代码,获取到要显示的坐标然后添加进入                       sydneyMarker.position=CLLocationCoordinate2DMake(model.latitude, model.longitude);
        sydneyMarker.map= mapView;
    }
}

这样就会在地图上显示每个坐标了

10、假如你需要一个悬窗怎么办,那你按照下面的代理事件做,方法如下:

设置图标需要设置

-(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
    return NO;**//必须设置为NO,不然点击图标的时候显示悬窗代理事件是不会走的**
}

点击图标的时候就会走这个代理事件,加载自定义的悬窗

-(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marke{
 return 自定义的view;
}

点击标记的信息窗口后调用。

-(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker{
       //这个代理事件是点击悬窗的时候做跳转事件,**注明,你的自定义view里面做了按钮什么的,是没有办法通过代理。通知等方法做跳转事件,必须在这个代理事件这里做跳转事件等逻辑**。
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值