0

Right now my app has multiple markers on different locations. If you tap on a marker, there is a small window popping out including a title and a snippet. I would like to implement a button in the window, or make the info window tappable, so it works as a button to execute a function. So I wrote this block in my GoogleMapsViewController.swift:

func mapView(mapView: GMSMapView, didTapInfoWindowOfMarker marker: GMSMarker) {

    print("test")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("jobDetailVC") as! JobDetailViewController

    if let value = marker.userData as? PFObject {
        vc.name = value.objectForKey("name") as? String
        vc.descriptionF = value.objectForKey("description") as? String
        vc.price = value.objectForKey("price") as? Double
        vc.objectId = value.objectId!
    }
}

The reason why I am using: didTapInfoWindowOfMarker is because I wasn't sure how to implement it, so I read the docu from Google Maps: https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p?hl=es and thought that this was the best choice.

Has anyone successfully implemented this, or something similar? Thanks in advance for the help!

1 Answers1

4

You are right in using didTapInfoWindowOfMarker function to add an event in your InfoWindow.

When you're adding Map, add:

 mapView_.delegate=self; 

Then use this to add the event/function of infoWindow when clicked:

-(void)mapView:(GMSMapView *)mapView
didTapInfoWindowOfMarker:(id<GMSMarker>)marker{

   //Info window function

}

Example on GitHub:

// when user tap the info window of store marker, show the product list
    func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!) {
        let storeMarker = marker as StoreMarker
        performSegueWithIdentifier("productMenu", sender: storeMarker.store)
    }

// when user tap the info window of store marker, pass selected store to the product list controller
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as ProductMenuController
        controller.store = sender as Store
    }

func mapView(mapView: GMSMapView, didTapInfoWindowOfMarker marker: GMSMarker) {

        for location in locations {
            let pollution = location[0]
            if pollution.locationdesc == marker.title {
                performSegueWithIdentifier(segueIdentifiers.locations, sender: location)
                break
            }
        }
    }

Check this related question:

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59