iOS 谷歌地图SDK小记

前期步骤参考谷歌官方文档

引入头文件:
#import 
#import 

引入代理协议

1.创建地图

  GMSMapView *mapView = [[GMSMapView alloc] initWithFrame:self.view.frame];
  mapView.delegate = self;
  [self.view addSubview: mapView];

2.定位当前位置

  // 显示官方的小蓝点,但是图标无法更改
  // 如果 iOS 提示用户允许他人访问此数据, 在这种情况下,它将为 nil (官方文档写的,也不解决很是郁闷)
  mapView.myLocationEnabled = YES;

  // 开启蓝点,可获取当前位置的坐标
  NSLog(@"User's location: %@", mapView.myLocation);


  上述这种方法会获取不到位置的情况,用下面一种方法代替

    GMSPlacesClient  *placesClient = [[GMSPlacesClient alloc] init];
    [placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
        if (error != nil) {
            NSLog(@"Current Place error %@", [error localizedDescription]);
            return;
        }
        if (likelihoodList != nil) {
            // 当前位置信息
            GMSPlace *place = [[[likelihoodList likelihoods] lastObject] place];
            
            if (place != nil) {
                // 创建地图显示中心  zoom为缩放比例
                GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:place.coordinate.latitude
                                                                        longitude:place.coordinate.longitude
                                                                             zoom:18];
                self.mapView.camera = camera;

                // 创建标记点
                GMSMarker *marker = [[GMSMarker alloc] init];
                marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude);
                 marker.map = mapView;
                 // 替换为你的图片
                 marker.icon = [UIImage imageNamed:@"point_gps"];

            }
        }
  
    }];

3.自定义信息窗口

  - (nullable UIView *) mapView:(GMSMapView *) mapView markerInfoWindow:(GMSMarker *) marker{

        //  DeviceInfoView自定义的信息窗口类,默认直接显示在点的中心位置
        DeviceInfoView *view = [[DeviceInfoView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
        [view initWithLocationModel:_currentLocation];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 6;
        return  view;
 }

4. 已进入地图直接显示信息窗口

   // marker 为你要显示的窗口的标记点
   mapView.selectedMarker = marker;

5. 清除地图上所有的标记点和信息

   [mapView clear];

6. 轨迹连线

    GMSMutablePath *path = [GMSMutablePath path];
    [path addCoordinate:CLLocationCoordinate2DMake(latitude1, longitude1)];
    [path addCoordinate:CLLocationCoordinate2DMake(latitude2, longitude2)];
    [path addCoordinate:CLLocationCoordinate2DMake(latitude3, longitude3)];

    GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
    rectangle.map = mapView;
    // 线的颜色
    rectangle.strokeColor = HexRGB(0xBBF0FF);
    // 线的宽度
    rectangle.strokeWidth = 3;

7. 画圆
    GMSCircle *circ = [GMSCircle circleWithPosition:marker.position
                                             radius:半径];
    // 圈内填充的颜色
    circ.fillColor = [UIColor colorWithRed:0.77 green:0.88 blue:0.94 alpha:0.8];
    // 圆边的颜色
    circ.strokeColor = [UIColor whiteColor];
    // 圆边的宽度
    circ.strokeWidth = 5;
    circ.map = mapView;

8. 点击地图获取位置信息
  // 只获取位置坐标点
  - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;

  // 获取位置信息详情
  - (void)mapView:(GMSMapView *)mapView didTapPOIWithPlaceID:(NSString *)placeID name:(NSString *)name location:(CLLocationCoordinate2D)location;


高德简直是业界良心!

你可能感兴趣的:(iOS 谷歌地图SDK小记)