gpt4 book ai didi

ios - 在 map 中显示多个标记

转载 作者:行者123 更新时间:2023-11-30 12:26:20 24 4
gpt4 key购买 nike

我正在使用谷歌地图制作一个应用程序,它在 map 上显示多个标记,我只想显示当前位置。

//
import UIKit
import GoogleMaps
import GooglePlaces
import GooglePlacePicker

class HomeLocationVC: UIViewController{


@IBOutlet var addressTextField: UITextField!
@IBOutlet var mapViewContainer: UIView!


var locationManager = CLLocationManager()
var currentLocation: CLLocation?
var mapView: GMSMapView!
var placesClient: GMSPlacesClient!
var zoomLevel: Float = 15.0
var likelyPlaces: [GMSPlace] = []
var selectedPlace: GMSPlace?
var camera:GMSCameraPosition?
var marker = GMSMarker()

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self
placesClient = GMSPlacesClient.shared()
userCurrentLocation()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}



@IBAction func searchWIthAddress(_ sender: Any) {
// Prepare the segue.
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToSelect" {
if let nextViewController = segue.destination as? PlacesViewController {
nextViewController.likelyPlaces = likelyPlaces
}
}
}

}


}

extension HomeLocationVC: CLLocationManagerDelegate {

// Handle incoming location events.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if let location = locations.first{
print("Location: \(location)")

camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
longitude: location.coordinate.longitude,
zoom: zoomLevel)

if mapView.isHidden {
mapView.isHidden = false
mapView.camera = camera!
} else {
mapView.animate(to: camera!)
}

listLikelyPlaces()
locationManager.stopUpdatingLocation()
}

let position = CLLocationCoordinate2D(latitude: (locations.last?.coordinate.latitude)!, longitude: (locations.last?.coordinate.longitude)!)
marker = GMSMarker(position: position)
marker.title = "Location"
marker.map = self.mapView
// marker.isTappable = true

}

// Handle authorization for the location manager.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted:
print("Location access was restricted.")
case .denied:
print("User denied access to location.")
// Display the map using the default location.
mapView.isHidden = false
case .notDetermined:
print("Location status not determined.")
case .authorizedAlways: fallthrough
case .authorizedWhenInUse:
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
print("Location status is OK.")
}
}

// Handle location manager errors.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
locationManager.stopUpdatingLocation()
print("Error: \(error)")
}
}


extension HomeLocationVC: GMSMapViewDelegate{

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
reverseGeocodeCoordinate(coordinate: position.target)
}
}

最佳答案

每次调用 func locationManager(_ manager: CLLocationManager, didUpdateLocationslocations: [CLLocation]) 时,您都会添加一个标记。即使通过调用 locationManager.stopUpdatingLocation() 仍然可能有待处理的更新。

您应该保留对单个标记的引用并更新其位置属性。

因此向类添加一个存储的属性

var marker: GSMMarker?

然后每次您收到新的位置更新时只需更新它即可。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let position = CLLocationCoordinate2D(latitude: (locations.last?.coordinate.latitude)!, longitude: (locations.last?.coordinate.longitude)!)
if let marker = self.marker {
marker = GMSMarker(position: position)
}
}

注意:上面的代码中有一个杂散括号,我想这只是一个复制错误,但它就在 func locationManager(_ manager 中的 locationManager.stopUpdatingLocation() 下:CLLocationManager,didUpdateLocations位置:[CLLocation])函数

关于ios - 在 map 中显示多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44162223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com