简体   繁体   English

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

[英]Location manager show bad location in gmsMapView swift 4.2

I have a problem that a I cannot resolve. 我有一个我无法解决的问题。 I'm trying to get the current location to update the mapview and center the camera, but when I close the view controller and init a new Mapviewcontroller the "current location" show a location incorrect, near to my position but not the current location. 我正在尝试获取当前位置以更新mapview并使相机居中,但是当我关闭视图控制器并初始化新的Mapviewcontroller时,“当前位置”显示的位置不正确,靠近我的位置,而不是当前位置。 Can anyone guide me o help me? 谁能指导我或帮助我? this is my code: 这是我的代码:

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()
}

}

This sample code can be your solution you can find here https://github.com/SwiftGuides/Google_Place_Picker 此示例代码可以作为您的解决方案,您可以在这里https://github.com/SwiftGuides/Google_Place_Picker

This is sample code i wrote for Google place picker to pick exact pin point custom location (not nearby location) 这是我为Google地点选择器写的示例代码,用于选择确切的定位点自定义位置(而不是附近的位置)

In your case you can apply this too. 您也可以应用此方法。 Just check the example code you will get you solution 只需检查示例代码即可获得解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM