0

I am currently trying to create a method to change the blue dot of googlemaps with a custom Icon, but currently I have the problem that the marker prints but don't clear its last location, this is the code I am using right now on with the "CLLocationManagerDelegate" and "GMSMap"

    //MARK: - location delegate methods

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if userLocation == nil {
        userLocation = manager.location!.coordinate
        let location = GMSCameraPosition.camera(withLatitude: userLocation.latitude, longitude: userLocation.longitude, zoom: 15)
        mapView.animate(to: location)

    }
    
    userLocation = manager.location!.coordinate
    let userLocationMarker = GMSMarker(position: manager.location!.coordinate)
    userLocationMarker.icon = UIImage(named: "est-my-location")
    userLocationMarker.map = mapView
}

enter image description here

Ricardo Guerrero
  • 433
  • 1
  • 5
  • 21

1 Answers1

0

Get Last Location from didUpdateLocations locations and update marker position.

Try Doing it this Way:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let newLocation = locations.last // find your device's Last location
    let position    = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0) // show your device location on map
    mapView.animate(to: position)

    let lat  = (newLocation?.coordinate.latitude)! // get current location latitude
    let long = (newLocation?.coordinate.longitude)! //get current location longitude
    
    userLocationMarker.icon     = UIImage(named: "est-my-location")
    userLocationMarker.position = CLLocationCoordinate2DMake(lat,long)
    
    DispatchQueue.main.async {
    userLocationMarker.map = mapView  // Setting marker on mapview in main thread.
    }
}
Kush Bhavsar
  • 909
  • 2
  • 12