2017-05-24 2 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()