Gmap: how to draw squares on Earth (lat, lon)

I tried to draw a square

rectangle = new google.maps.Rectangle(); var rectOptions = { strokeColor : "#FF0000", strokeOpacity : 0.8, strokeWeight : 2, fillOpacity : 0, map : map }; rectangle.setOptions(rectOptions); 

with a length of 0.005 '

 google.maps.event.addListener(map, 'dragend', function() { var center = map.getCenter(); rectangle.setBounds(new google.maps.LatLngBounds( new google.maps.LatLng(center.lat()-0.005, center.lng()-0.005), new google.maps.LatLng(center.lat()+0.005, center.lng()+0.005))); }); 

but I realized that the square is a square near the equator, but it is quite deformed elsewhere, is there a function that decreases with latitude to get the equivalent length of longitude? thanks for reference

edit : square height = cos (lat) width

+4
source share
1 answer

from Andrew Leach comment:

 function drawSquare(position=new google.maps.LatLng(41, 36.12), radius=0.001){ var rectangle = new google.maps.Rectangle(); var rectOptions = { strokeColor : "#FF0000", strokeOpacity : 0.8, strokeWeight : 2, fillOpacity : 0 }; rectangle.setOptions(rectOptions); var c = Math.cos(position.lat()* Math.PI / 180); rectangle.setBounds(new google.maps.LatLngBounds( new google.maps.LatLng(position.lat()-c*radius/2, position.lng()-radius/2), new google.maps.LatLng(position.lat()+c*radius/2, position.lng()+radius/2))); return rectangle; } 
0
source

All Articles