1

Google Maps API works, all binary libraries linked, -Obj in target's "other linker flags" included and objc header file constructed, and this code is basically taken straight off the google developers demo for how to add event tapping. Yet, for some reason - even though my map and polygon and marker work - I cannot figure out how to make my single polygon tappable. (I eventually want to make it change from red to green, and back upon user tap). I am a beginner in swift and xcode, using the latest updates, and could really use some help!

Here is my code:

class ViewController: UIViewController, GMSMapViewDelegate {

override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: 40.0, longitude: -75.3, zoom: 17.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.delegate = self
    self.view = mapView

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 40.0, longitude: -75.3)
    marker.title = "University1"
    marker.isTappable = true
    marker.map = mapView

    // Create a rectangular path
    let K1 = GMSMutablePath()
    K1.add(CLLocationCoordinate2D(latitude: 40.04080470744396, longitude: -75.34219389549136))
    K1.add(CLLocationCoordinate2D(latitude: 40.04082335573317, longitude: -75.34220634745961))
    K1.add(CLLocationCoordinate2D(latitude: 40.0407983689258, longitude: -75.34226262271473))
    K1.add(CLLocationCoordinate2D(latitude: 40.04078009018794, longitude: -75.34224985832812))
    let K1polygon = GMSPolygon(path: K1)
    K1polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
    K1polygon.strokeColor = .red
    K1polygon.strokeWidth = 2
    K1polygon.isTappable = true
    K1polygon.map = mapView

}}
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print("You did it!")
}

let geocoder = GMSGeocoder()

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
mapView.clear()
}

func mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition)    {
geocoder.reverseGeocodeCoordinate(cameraPosition.target) { (response, error)    in
    guard error == nil else {
        return
    }

    if let result = response?.firstResult() {
        let marker = GMSMarker()
        marker.position = cameraPosition.target
        marker.title = result.lines?[0]
        marker.snippet = result.lines?[1]
        marker.map = mapView
    }
}}
  • http://stackoverflow.com/questions/39977363/get-lat-and-long-from-tapped-overlay-in-google-maps/40583753#40583753 – GIJOW Apr 26 '17 at 20:50

0 Answers0