Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

javascript - How can I check the marker is or isn't in the bounds using google maps v3

This is my code, I think it may has some mistakes:

        var bounds_array;
        google.maps.event.addListener(map,'bounds_changed', function (){
            var bounds_=map.getBounds();
            if(bounds_){
                var leftBottom=[bounds_.getSouthWest().lat(),bounds_.getSouthWest().lng()]
                var rightTop=[bounds_.getNorthEast().lat(),bounds_.getNorthEast().lng()]
                bounds_array=[leftBottom,rightTop];
            }
        });

    function check_is_in_or_out(marker){
        var leftBottom=bounds_array[0],rightTop=bounds_array[1];
        var marker_p=[marker.getPosition().lat(),marker.getPosition().lng()];
        if(marker_p[0]<leftBottom[0]||marker_p[0]>rightTop[0]||
            marker_p[1]<leftBottom[1]||marker_p[1]>rightTop[1])return 0;//0 is out
        else return 1;//1 is in
    }

Is this code enough to check the bounds is in or out?

question from:https://stackoverflow.com/questions/3648545/how-can-i-check-the-marker-is-or-isnt-in-the-bounds-using-google-maps-v3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The LatLngBounds object comes with a contains() method which takes a LatLng point and returns true if the point happens to be within the bounds, or false if outside.

Therefore, what about something like the following?

function check_is_in_or_out(marker){
  return map.getBounds().contains(marker.getPosition());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...