There is such code:

google.maps.event.addDomListener(window, 'load', function(){ var mapCanvas = document.getElementById("map-canvas"); var map = new google.maps.Map(mapCanvas, { center: new google.maps.LatLng(37.778042,-122.419136), zoom: 5, mapTypeId: google.maps.MapTypeId.TERRAIN, panControl: true }); var drawingManager = new google.maps.drawing.DrawingManager({ drawingControl: true, drawingControlOptions: { position: google.maps.ControlPosition.TOP_CENTER, drawingModes: [google.maps.drawing.OverlayType.RECTANGLE] }, drawingMode: google.maps.drawing.OverlayType.RECTANGLE }); drawingManager.setMap(map); google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(rect) { var bounds = rect.getBounds(); console.log(bounds.getNorthEast().lat() + ', ' + bounds.getNorthEast().lng()); console.log(bounds.getSouthWest().lat() + ', ' + bounds.getSouthWest().lng()); }); }); 

Here I get the coordinates of two points of the rectangle drawn on the map. At these two points, I derive the latitude and longitude. The database stores images with coordinates. I need to compare each of them to hit a rectangle area. How is it better and easier to do? There is an idea to use the intersects method. That is something like this:

let's say we already have an array of images images and each of them has a bounds property.

 for(var i = 0; i < images; i++){ if(bounds.intersects(images[i].bounds)){ console.log('match'); } } 

I have not checked this code yet, but I want to know if I am going in the right direction?

    1 answer 1

    In the right, the intersects method is precisely for this purpose, only you have something missing in the condition of the cycle.

     for (var i = 0, img; (img = images[i]) !== undefined; i++) { if(bounds.intersects(img.bounds)) { console.log('true'); } } 
    • Yes, I forgot to add length in a loop. I don’t really understand your cycle. - Lucky
    • one
      In the examples on Google maps you can often find such a recording format. Sometimes it saves one line of code. If you have a frequent reference to the current element of the arr [i] cycle inside the loop, then most likely you, inside the loop, define the variable elem = arr [i]. In the example I wrote you, the definition of this variable itself occurs in the loop itself. - Ridzhi
    • Simply put, this is caching the value in a variable. An interesting approach. - Lucky