Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 558
zjm1126 Avatar asked Sep 06 '10 01:09

zjm1126


People also ask

How do I find the marker ID on Google Maps?

var marker = new google. maps. Marker(markerOptions); marker. metadata = {type: "point", id: 1};

How do you get bounds on Google Maps?

getBounds() in Google Maps API v3 But in API v3 you will get “bounds is undefined” error. So to get our latitude and longitude we need to move getBounds(), to some event listener. Description of bounds_changed in documentation is: “This event is fired when the viewport bounds have changed.”

How do you show markers on a map?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

What do the markers mean on Google Maps?

A marker identifies a location on a map. By default, a marker uses a standard image. Markers can display custom images, in which case they are usually referred to as "icons." Markers and icons are objects of type Marker . You can set a custom icon within the marker's constructor, or by calling setIcon() on the marker.


1 Answers

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()); } 
like image 94
Daniel Vassallo Avatar answered Sep 28 '22 20:09

Daniel Vassallo