我有一张地图,可使用google maps sdk显示当前位置。地图设置在此代码中
override func viewDidAppear(_ animated: Bool) {
if CLLocationManager.locationServicesEnabled() {
startMonitoringLocation()
addCurrentLocationMarker()
}
}
func startMonitoringLocation() {
if CLLocationManager.locationServicesEnabled() {
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.activityType = CLActivityType.automotiveNavigation
locationManager.distanceFilter = 1
locationManager.headingFilter = 1
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
locationManager.startUpdatingLocation()
}
}
func stopMonitoringLocation() {
locationManager.stopMonitoringSignificantLocationChanges()
locationManager.stopUpdatingLocation()
}
func addCurrentLocationMarker() {
currentLocationMarker?.map = nil
currentLocationMarker = nil
if let location = locationManager.location {
currentLocationMarker = GMSMarker(position: location.coordinate)
currentLocationMarker?.icon = #imageLiteral(resourceName: "Geolocalización")
currentLocationMarker?.map = mapView
currentLocationMarker?.rotation = locationManager.location?.course ?? 0
}
}
func zoomToCoordinates(_ coordinates: CLLocationCoordinate2D) {
let camera = GMSCameraPosition.camera(withLatitude: coordinates.latitude, longitude: coordinates.longitude, zoom: 14)
mapView.camera = camera
}
//MARK:- Location Manager Delegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("location manager erroe -> \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
break
case .restricted:
break
case .denied:
stopMonitoringLocation()
break
default:
addCurrentLocationMarker()
startMonitoringLocation()
break
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("location update")
if let lastLocation = locations.last {
currentLocationMarker?.position = lastLocation.coordinate
currentLocationMarker?.rotation = lastLocation.course
self.zoomToCoordinates(lastLocation.coordinate)
self.stopMonitoringLocation()
}
}
因此,当我使用地点自动完成功能选择地点时,我想在该位置显示标记,相机将移至该地点,然后返回到当前位置。显示新标记的地方完整代码是这个。
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// print("Place name: \(place.name)")
// print("Place address: \(place.formattedAddress)")
// print("Place attributions: \(place.attributions)")
if punto1{
print("is text1")
punto1Text.text = place.formattedAddress
punto1Cordinate = place.coordinate//place.viewport?.southWest
let newMarker = GMSMarker(position: punto1Cordinate!)
newMarker.icon = UIImage(named: "desde")
newMarker.map = self.mapView
self.mapView.animate(toLocation: punto1Cordinate!)
self.mapView.camera = GMSCameraPosition.camera(withTarget: punto1Cordinate!, zoom: 14.0)
//self.zoomToCoordinates(punto1Cordinate!)
if punto2Cordinate != nil{
self.drawPath(origin: "\(punto1Cordinate!.latitude),\(punto1Cordinate!.longitude)", destination: "\(punto2Cordinate!.latitude),\(punto2Cordinate!.longitude)")
let mapsBound = GMSCoordinateBounds(coordinate: punto1Cordinate!, coordinate: punto2Cordinate!)
//self.mapView.camera(for: mapsBound, insets: <#T##UIEdgeInsets#>)
// self.mapView.cameraTargetBounds = mapsBound
self.mapView.animate(with: GMSCameraUpdate.fit(mapsBound, withPadding: 120.0))
}
}else{
print("is text2")
punto2Text.text = place.formattedAddress
punto2Cordinate = place.coordinate//viewport?.southWest
let newMarker = GMSMarker(position: punto2Cordinate!)
newMarker.icon = UIImage(named: "hasta")
newMarker.map = self.mapView
if punto1Cordinate != nil {
self.drawPath(origin: "\(punto1Cordinate!.latitude),\(punto1Cordinate!.longitude)", destination: "\(punto2Cordinate!.latitude),\(punto2Cordinate!.longitude)")
let mapsBound = GMSCoordinateBounds(coordinate: punto1Cordinate!, coordinate: punto2Cordinate!)
//self.mapView.camera(for: mapsBound, insets: <#T##UIEdgeInsets#>)
// self.mapView.cameraTargetBounds = mapsBound
self.mapView.animate(with: GMSCameraUpdate.fit(mapsBound, withPadding: 120.0))
//self.mapView.camera = GMSCameraPosition.
}
}
dismiss(animated: true, completion: nil)
}
当有两个点时,相机必须在两个点之间的路线中居中,但相机无法移动。 我已停止使用此功能更新位置。
func textFieldDidBeginEditing(_ textField: UITextField) {
self.stopMonitoringLocation()
if textField == punto1Text{
punto1 = true
}else{
punto1 = false
}
self.servicioOrigen.isHidden = true
self.servicioHoras.isHidden = true
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
let filter = GMSAutocompleteFilter()
filter.country = "es"
//filter.
//filter.type = .region
//filter.type = .address
autocompleteController.autocompleteFilter = filter
present(autocompleteController, animated: true, completion: nil)
}
如果禁用了位置,则动画将在firstPoint中工作,添加第二点后,我必须移动地图以使其位于路线中心,但是设备位置处于打开状态,则相机无法对焦选定的位置。