Get LAT and LONG from Overlay on Google Maps

When the user removes the overlay, the following code is run:

func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {


    }

I wonder if we can extract the exact LAT and LONG coordinates of the overlay?

Thank!

+1
source share
2 answers

To solve this problem, we need two methods together, so I combined them in this way, I hope this helps in this matter:

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)
        }
    }
}

if the user clicked on coordinate, we can handle this. Then check if this one is contained coordinatein any Polylineor Polygonthat we defined earlier. So, we are an fireevent didTap overlayfor this overlay.

Be sure to do polylinesandpolygons isTappable = false

, overlay , overlaped, return, if overlay

+1

, , Overlay , GMSMapViewDelegate, , GoogleMaps. , , , .

Swift 3.0

 func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        print(coordinate.latitude)
        print(coordinate.longitude)
    }

,

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        print(marker.position.latitude)
        print(marker.position.longitude)
        return true
    }

,

overlay.isTappable = false

.

0

Source: https://habr.com/ru/post/1541956/


All Articles