如何根据当前的mapView动态存储纬度和经度坐标?

时间:2017-03-19 11:47:05

标签: ios objective-c xcode

如果用户点击缩小或放大按钮,我会执行以下代码:

 - (void)zoomHandler:(CGFloat)currentZoom
 {
GMSCameraPosition *camera = [GMSCameraPosition **cameraWithLatitude:40.790218 longitude:-73.959722** zoom:currentZoom];
self.mapView.camera = camera;
 }

但是,如果用户离开上述默认坐标并单击任一按钮,他/她将被带回上述默认坐标。我希望用户能够在显示的当前mapView上放大/缩小 - 而不是自动恢复到上述方法中声明的坐标。

我如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

创建两个变量来存储当前坐标位置:

CLLocationDegrees latitude,longitude;

       -(void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position
        {
        CGPoint point = self.mapView.center;
        CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point];
        longitude = coor.longitude;
        latitude = coor.latitude;
        }

您可以使用以下方法中的代码执行缩放:

        -(void)zoomInAction {
        CGFloat currentZoom = self.mapView.camera.zoom;
        currentZoom = currentZoom + 1.0;

        if(currentZoom < self.mapView.maxZoom) {
          GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                                  longitude:longitude
                                                                       zoom:currentZoom];
          self.mapView.camera = camera;
          [self.mapView animateToCameraPosition:camera];
          }
        }

        -(void)zoomOutAction {
        CGFloat currentZoom = self.mapView.camera.zoom;
        currentZoom = currentZoom - 1.0;
        if(currentZoom > self.mapView.minZoom) {
         GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                                 longitude:longitude
                                                                      zoom:currentZoom];
           self.mapView.camera = camera;
           [self.mapView animateToCameraPosition:camera];
           }
         }