2017-05-24 19 views
0

Googleマップでアプリを作っています。地図上に複数のマーカーが表示されています。現在の位置のみを表示したいです。複数のマーカーを地図に表示

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

答えて

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

注:上記のコード内の浮遊ブラケットがあります、私はそれだけでコピーエラーになります想像しますが、func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])機能でそのすぐ下にlocationManager.stopUpdatingLocation()

関連する問題