在地图中显示多个标记

时间:2017-05-24 15:04:54

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

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

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

1 个答案:

答案 0 :(得分:0)

每次调用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)
    }
}

注意:上面的代码中有一个迷路括号,我想它只是一个复制错误,但它位于locationManager.stopUpdatingLocation()函数中的func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])