当移动时,如何将当前位置点保持在地图的中心,谷歌地图,快速

时间:2018-01-29 05:11:31

标签: swift google-maps-sdk-ios

在我的项目中,我正在使用谷歌地图。我正在启用当前位置点:

 self.mapView?.isMyLocationEnabled = true

之后我得到当前位置的纬度和经度:

 guard let currentLat = self.mapView?.myLocation?.coordinate.latitude,
          let currentLan = self.mapView?.myLocation?.coordinate.longitude else {
          showMessageFail(title: "Fail", myMessage: "Could not determine your current location")
          return
        }

然后将相机对准当前位置,如下所示:

  let currentPosition = CLLocationCoordinate2D(latitude: currentLat, longitude: currentLan)
  self.mapView?.animate(toLocation: self.currentPosition)

一切都很好,现在它显示了地图中心的当前位置点(谷歌地图上的蓝点)。现在,当当前位置发生变化时,说我在汽车中当前位置点移动,在某些时候它移出地图的边界。如何始终将当前位置点保持在地图中心并移动地图而不是点。任何帮助将非常感激。

2 个答案:

答案 0 :(得分:0)

currLocation = manager.location!

    let locationOfDevice: CLLocation = currLocation // this determines the location of the device using the users location

    let deviceCoordinate: CLLocationCoordinate2D = locationOfDevice.coordinate // determines the coordinates of the device using the location device variabel which has in it the user location.

    let span = MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) // this determines the span in which its determined that 1 degree is 69 miles.

    let region = MKCoordinateRegion(center: deviceCoordinate, span: span) // set the center equal to the device coordinates and the span equal to the span variable we have created this will give you the region.

    mapView.setRegion(region, animated: true);

答案 1 :(得分:0)

CLLocation *newMyLocation = [[CLLocation alloc] initWithLatitude:_locationManager.location.coordinate.latitude  longitude:_locationManager.location.coordinate.longitude];



                         _cmarker.position = newMyLocation.coordinate;

                         CGPoint point1 =[_mapkitView.projection pointForCoordinate:newMyLocation.coordinate];

                         point1.y = -400;

                         GMSCameraUpdate *cameraUpdate = [GMSCameraUpdate setTarget:[_mapkitView.projection coordinateForPoint:point1]];
                         [_mapkitView animateWithCameraUpdate:cameraUpdate];
                         _cmarker.map = self.mapkitView;



                         //[_mapkitView setCenterCoordinate:newMyLocation.coordinate zoomLevel:_mapkitView.zoomLevel animated:YES];

                         [_mapkitView animateToLocation:CLLocationCoordinate2DMake(newMyLocation.coordinate.latitude, newMyLocation.coordinate.longitude)];
                         [_mapkitView animateToZoom:_mapkitView.camera.zoom]; // animate to that zoom level