0

I have a marker called chicagoMarker and each time the user zooms in closer I would like to alert them if they are getting closer to chicago. I am not sure if I have a problem with my event listener in this instance.

if (score==0) {
    chicagoMarker.setMap(map)
    google.maps.event.addListener(chicagoMarker,'bounds_changed',function(){
        zoom = map.getZoom();
        if (bounds.contains(chicagoMarker)){
            window.alert("Good Job!");
            }
        })

}
1
  • try troubleshooting by moving the window.alert() before .setmap(), or even before (score==0)... does this produce an alert?
    – kttii
    Commented Nov 11, 2016 at 14:14

1 Answer 1

1

Not sure about your code... You create a variable zoom, but then don't use it. You refer to a variable bounds, but I don't see that being created anywhere. You have an event listener set up on the Marker for 'bounds_changed', but the Marker class hasn't got that as an event you can listen for. See the list of events available under 'Events' here: https://developers.google.com/maps/documentation/javascript/reference#Marker

Instead set the event listener on the Map object, for the same event. And I've changed it to see if the Marker's position is inside the Bounds:

if (score == 0) {
    chicagoMarker.setMap(map);

    map.addListener('bounds_changed', function() {
        var bounds = map.getBounds();
        if (bounds.contains(chicagoMarker.getPosition())) {
            window.alert("Good Job!");
        }
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.