Touch end event on GoogleMap iOS Swift - ios

I am trying to work on Google Map with some UIView. My requirement is : If I touch the MapView, one of UIView will disappear(animated) and it will remain disappear until I release the touch from MapView. I managed to disappear the UIView from screen on touching the MapView using the below code :
func mapView(_ mapView: GMSMapView, didLongPressAt
coordinate: CLLocationCoordinate2D) {
self.animate()
}
func mapView(_ mapView: GMSMapView,
idleAt position: GMSCameraPosition) {
self.collapse()
}
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
//
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
On dragging map and release { Touch --> Drag --> Lift } I can detect Finger Lift using idleAt. But I couldn't detect with anything if I { Touch --> hold --> Lift }. You can consider Uber or Ola app MapView for example

You can use GoogleMaps GMSMapViewDelegate:
mapView.delegate = self
This will call when Google map start moving and idle.
func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
//
}
// Touch drag and lift
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
//
}
// Touch and lift
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print("yes")
}

Related

When a user taps an overlay, the following code is triggered:

I wonder if we can extract the exact LAT and LONG coordinates of the overlay that has been tapped?
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
}
I want to get the exact coordinates, but I cannot get the coordinates when tapping on the overlay.
Thanks!
You can try the below code.
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print(overlay)
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print(coordinate)
for polyline in polylines {
if GMSGeometryIsLocationOnPath(coordinate, polyline.path!, true) {
self.mapView(mapView, didTap: polyline)
}
}
for polygon in polygons {
if GMSGeometryContainsLocation(coordinate, polygon.path!, true) {
self.mapView(mapView, didTap: polygon)
}
}
}
Similar question was asked. And this is an answer from older thread. Get LAT and LONG from tapped overlay in Google Maps

Listen for GoogleMaps Marker unselected swift

I am trying to detect when a Google Maps marker has been unselected so that I can toggle the visibility of a button.
Below is my code to determine if a marker has been clicked.
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!){
joinButton.isHidden = false
}
How can I determine when the marker has been unselected? I want my button only to appear when a user has clicked the marker.
This is how it was implemented in Android:
mMap.setOnInfoWindowCloseListener(new GoogleMap.OnInfoWindowCloseListener() {
#Override
public void onInfoWindowClose(Marker marker) {
joinButton.setClickable(false);
joinButton.setVisibility(View.INVISIBLE);
}
});
Found a solution, the first method should be:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
joinButton.isHidden = false
}
A marker is unselected when any other point of the map is clicked, so:
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
joinButton.isHidden = true
}

Why doesn't my 'didTap marker' function work?

I have been building a Google Maps iOS app and somehow my 'didTap marker' function does not work. If I tap on a marker on the map, the whole view simply just gets dragged off to left, but no code gets run. Otherwise, the print functions below would work. Where might cause this problem?
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker){
var lat: CLLocationDegrees = marker.position.latitude
var lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
print("markersArray is \(markersArray)")
print("formattedCoordinate is \(formattedCoordinate)")
markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
}
You need to set delegate for mapview.
set delegate in viewDidLoad
myGMSMapView.delegate = self
then use this method
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
<#code#>
}
Add this delegate in your class.
GMSMapViewDelegate
Now, this will be your marker action.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.delegate = self
print("You tapped : \(marker.position.latitude),\(marker.position.longitude)")
}

How to clickable for markers in google map

I'm using google map and I set markers to my map like this:
var marker = GMSMarker(position: CLLocationCoordinate2D(latitude: Double(item.lat)!, longitude: Double(item.lon)!))
marker.map = mapview
now,I would like to detect when user click on these markers.
How can I do?
you should set your mapview delegate to self UIViewController in viewDidLoad
self.mapview.delegate = self
your UIViewController should
extension ViewControllerClass: GMSMapViewDelegate {
//class code
#objc(mapView:didTapMarker:) func mapView(_: GMSMapView, didTap marker: GMSMarker) -> Bool {
//do something
return true
}
}
maybe this method can be implemented some other way already, but Xcode forced me to make it this way while migrating from Swift 2 to Swift 3
For Swift 3
You can implement GMSMapViewDelegatesomething like this:
extension YourViewConytoller: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print ("MarkerTapped Locations: \(marker.position.latitude), \(marker.position.longitude)")
return true
}
}

Listen for InfowWindow tap in Swift

I know it is possible to capture the taps in the infowindow of a marker. I followed the documentation here.
All are written in Objective C so I tried converting it to Swift, here is my code:
func mapView(_ mapView: GMSMapView, didTap InfoWindowOfMarker: GMSMarker) {
print("You tapped infowindow")
}
But this isn't getting fired at all. What is wrong with the method?
You need to use the delegate of GMSMapView along with some prior setting see below.
Declare the use of GMSMapViewDelegate methods and set the delegate to self:
class yourClassName: UIViewController,GMSMapViewDelegate
mapView?.delegate = self
Method to detect tap on infoWindow:
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
print("infowindow tapped")
}
Method to detect tap on GMSMarker:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
print("tapped on marker")
if marker.title == "myMarker"{
print("handle specific marker")
}
return true
}
Method to create custom infoWindow:
func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
let infoWindow = Bundle.main.loadNibNamed("nibName", owner: self, options: nil).first as! ClassName
infoWindow.name.text = "title"
infoWindow.address.text = "relevant address"
infoWindow.photo.image = UIImage(named: "imageName")
return infoWindow
}

Resources