Alexey K
Alexey K

Reputation: 6723

Swift google maps - how to move marker at the bottom of the screen when marker tapped?

I'm playing with google maps and I need to override default behavior of tapping on the marker - by default marker goes to center of the screen and infowindow is being shown above. I need to figure out a way that when marker is tapped marker moves to the bottom of screen and infowindow shows on center. I found solutions here but I couldnt translate it to to swift

Any help please?

Upvotes: 2

Views: 4597

Answers (2)

J.Arji
J.Arji

Reputation: 661

add GMSMapViewDelegate

 // move marker


   func updateLocationoordinates(coordinates:CLLocationCoordinate2D) {
        if marker == nil
        {
            marker = GMSMarker()
            marker.position = coordinates
            let image = UIImage(named:"destinationmarker")
            marker.icon = image
            marker.map = mapPageView
            marker.appearAnimation = GMSMarkerAnimation.pop
        }
        else
        {
            CATransaction.begin()
//            CATransaction.setAnimationDuration(0.1)
            marker.position =  coordinates
            CATransaction.commit()
        }
        print(coordinates)
    }

Camera change Position this methods will call every time

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

    var destinationLocation = CLLocation()
    destinationLocation = CLLocation(latitude: position.target.latitude,  longitude: position.target.longitude)
    destinationCoordinate = destinationLocation.coordinate
    updateLocationoordinates(coordinates: destinationCoordinate)

}

good luck

Upvotes: 1

Alexey K
Alexey K

Reputation: 6723

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {

    mapView.animateToLocation(marker.position)
    mapView.selectedMarker = marker

    var point = mapView.projection.pointForCoordinate(marker.position)
    point.y = point.y - 200

    var newPoint = mapView.projection.coordinateForPoint(point)
    var camera = GMSCameraUpdate.setTarget(newPoint)
    mapView.animateWithCameraUpdate(camera)

    return true

}

Upvotes: 6

Related Questions