位置管理器在gmsMapView swift 4.2中显示错误的位置

问题描述 投票:0回答:1

我有一个问题,我无法解决。我正在尝试获取当前位置以更新mapview并使摄像机居中,但是当我关闭视图控制器并初始化一个新的Mapviewcontroller时,“当前位置”显示的位置不正确,靠近我的位置但不是当前位置。任何人都可以指导我帮助我吗?这是我的代码:

class MapContainerViewController: UIViewController{
@IBOutlet weak var mapView: GMSMapView!

var locationManager = CLLocationManager()
var myLocation: CLLocation?
var autorizeChange: Bool?
var geofire: GeoFire?
var createGeoforme: Bool?
var INITIAL_CENTER: CLLocation?
var searchCircle: GMSCircle?
var timer = Timer()

override func viewDidLoad() {
    super.viewDidLoad()
    autorizeChange = true
    checkLocationServices()
    createGeoforme = false
    mapView.delegate = self

    // Do any additional setup after loading the view.
  }


 func setupLocationManager(){
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = 100
    locationManager.pausesLocationUpdatesAutomatically = false
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

func checkLocationServices(){
    if CLLocationManager.locationServicesEnabled(){
        setupLocationManager()
        checkLocationAutorization()
    } else {

    }
}

func checkLocationAutorization(){
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        //Do map stuff
        if (mapView.isMyLocationEnabled == false ){
            mapView.isMyLocationEnabled = true
            mapView.settings.myLocationButton = true
            var ref: DatabaseReference
            ref = Database.database().reference()
            geofire = GeoFire(firebaseRef: ref)              

        }
        break
    case .denied:
        //Show alert instructing
        break
    case .notDetermined:
        //location manager
        locationManager.requestWhenInUseAuthorization()
        break
    case .restricted:
        //Show alert letting them know
        break
    case .authorizedAlways:
        break
    default:

    }
}
}

}

extension MapContainerViewController: GMSMapViewDelegate{

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) 
 {
    mapView.clear()
    let coordinate = mapView.projection.coordinate(for: mapView.center)
    var cooordinate2d = CLLocationCoordinate2D.init(latitude: coordinate.latitude, longitude: coordinate.longitude)
    var circ = GMSCircle.init(position: coordinate, radius: zoomLevelToRadius(zoomLevel: Double(position.zoom)))
    circ.fillColor = UIColor.init(displayP3Red: 0, green: 255, blue: 255, alpha: 0.15)
    circ.strokeColor = UIColor.init(displayP3Red: 0, green: 0, blue: 0, alpha: 0.15)
    circ.strokeWidth = 0.5
    circ.map = mapView;

}

func zoomLevelToRadius(zoomLevel: Double) -> Double{
// Approximation to fit circle into view
    return (16384000/pow(2, zoomLevel))
}
}

extension MapContainerViewController: CLLocationManagerDelegate{

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation: CLLocation = locations[0]
    let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    mapView.camera = GMSCameraPosition.camera(withTarget: location, zoom: 14)
    let centerLocation = mapView.projection.coordinate(for: mapView.center)
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    checkLocationAutorization()
}

}
swift google-maps delegates cllocationmanager gmsmapview
1个回答
0
投票

这个示例代码可以是您在这里找到的解决方案https://github.com/SwiftGuides/Google_Place_Picker

这是我为Google地方选择器编写的示例代码,用于选择确切的针点自定义位置(不在附近位置)

在您的情况下,您也可以应用它。只需检查示例代码,您就可以获得解决方案

© www.soinside.com 2019 - 2024. All rights reserved.