Changing GMSMarker icon doesn't work

1.7k views Asked by At

I'm trying to change GMSMarker icon with Moa but marker is still showing default icon. This is my code:

   override func viewDidLoad() {
        super.viewDidLoad()
        locationmanager.delegate = self
        let authorizationStatus = CLLocationManager.authorizationStatus()
        if(authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways) {
            locationmanager.startUpdatingLocation()
            MapView.isMyLocationEnabled = true
            MapView.settings.myLocationButton = true
        } else {
            locationmanager.requestWhenInUseAuthorization()
        }
        MapView.camera = GMSCameraPosition.camera(withLatitude: 32.4279, longitude: 53.6880, zoom: 5)
        MapView.delegate = self
        prepareMapMarkers()
    }        
    func prepareMapMarkers(){
        let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: CLLocationDegrees(malls[0].lat), longitude: CLLocationDegrees(malls[0].long)))
        marker.map = MapView
        moa.onSuccess = { image in
            print(image)
            marker.icon = image.images?[0]
            return image
        }
        moa.url = "http://www.w3schools.com/css/img_fjords.jpg"
    }

console

<UIImage: 0x60000009e050>, {600, 400}

That image is just for test. Why it's not working?

2

There are 2 answers

1
Prabhu.Somasundaram On

Try this! It worked for me

let pin = UIImage(named: "maps_icon_location")!.imageWithRenderingMode(.AlwaysTemplate)
                                marker.icon = pin
2
Andrew Walz On

You are attempting to change the image of a marker after it is added. I am not sure if Google Maps SDK supports this, but it is not a good idea. Instead, download you photo, then create the marker and add it to the map:

moa.onSuccess = { image in
    let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: CLLocationDegrees(malls[0].lat), longitude: CLLocationDegrees(malls[0].long)))
    marker.icon = image.images?[0]
    marker.map = self.MapView
}
moa.url = "http://www.w3schools.com/css/img_fjords.jpg"