Daibaku
Daibaku

Reputation: 12566

Google map view color error

I would like to change marker position if button tapped. I tried like below. but it will look like this. Some how color will be changed to like so. How can I fix this? Thank you!

enter image description here

@objc func changeMarker() {
        let next = CLLocationCoordinate2DMake(40.730610, -73.935242)
        mapView.camera = GMSCameraPosition.camera(withLatitude: next.latitude, longitude: next.longitude, zoom: 13)
        let marker = GMSMarker(position: next)
        marker.title = "over here"
        marker.map = mapView
    }

Upvotes: 0

Views: 44

Answers (1)

kuzdu
kuzdu

Reputation: 7524

Add this marker and try this

    func addMarker() {
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(yourCoordinateLatitude), longitude: CLLocationDegrees(yourCoordinateLongitude))
        marker.map = mapView
    }

Now you can zoom to your marker

func zoomToCoordinate(coordinate: CLLocationCoordinate2D, zoom: Float) {
        CATransaction.begin()
        CATransaction.setValue(1, forKey: kCATransactionAnimationDuration)
        let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: zoom)
        self.animate(to: camera)
        CATransaction.commit()
    }

I would call it like this

mapView.clear() // to clear your mapView
mapView.addMarker()
mapView.zoomToCoordinate(coordinate: yourCoordinate, zoom: 15)

Always check if your function is called and if the coordinations are correct.

This code works in my project. If it doesn't work for your project please share more code from you.

Upvotes: 1

Related Questions