我有一个我无法解决的问题。我正在尝试获取当前位置以更新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()
}
}
答案 0 :(得分:0)
此示例代码可以作为您的解决方案,您可以在这里https://github.com/SwiftGuides/Google_Place_Picker
这是我为Google地点选择器写的示例代码,用于选择确切的定位点自定义位置(而不是附近位置)
您也可以应用此方法。只需检查示例代码即可获得解决方案