2017-05-24 13 views
0

Ich mache eine App mit Google Map zeigt mehrere Marker auf Karte Ich möchte nur für den aktuellen Standort zeigen.Zeige mehrere Marker in Karte

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

Antwort

0

Jedes Mal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) wird Sie einen Marker ADD genannt. Selbst beim Aufruf von locationManager.stopUpdatingLocation() könnten noch Updates ausstehen.

Sie sollten einen Verweis auf einen einzelnen Marker beibehalten und stattdessen seine Positionseigenschaft aktualisieren.

so eine gespeicherte Eigenschaft der Klasse hinzufügen

var marker: GSMMarker? 

und dann jede Sie neue Position erhalten Updates es nur aktualisieren.

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

Hinweis: Es gibt eine Streu Klammer im Code oben, stelle ich mir es wird nur eine Kopie Fehler sein, aber es ist nur unter locationManager.stopUpdatingLocation() in der func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) Funktion