【问题标题】:Showing multiple Marker in map在地图中显示多个标记
【发布时间】:2017-10-25 00:35:45
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift google-maps-api-3


    【解决方案1】:

    每次func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [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: CLLocationManager, didUpdateLocations locations: [CLLocation]) 函数中的 locationManager.stopUpdatingLocation() 下方

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      相关资源
      最近更新 更多