Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer to your question can be found in the <a href="https://developers.google.com/maps/documentation/javascript/reference" rel="nofollow noreferrer">Google Maps API (v3)</a>.</p> <p>The basic approach here is:</p> <ol> <li>Find the bounds of the map using the getBounds() method of the <a href="https://developers.google.com/maps/documentation/javascript/reference#Map" rel="nofollow noreferrer">Map object</a>. The result is a <a href="https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds" rel="nofollow noreferrer">LatLngBounds object</a>, from which you can extract the latitude and longitude coordinates of the corners of the map.</li> <li><a href="https://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points">Compute the distance</a> in (kilo)meters between the north and south, and west and east of the map. Use this distance to determine how many lines (with distance of 1km) you should draw.</li> <li>Draw the grid in the shape of <a href="https://developers.google.com/maps/documentation/javascript/reference#Polyline" rel="nofollow noreferrer">PolyLines</a>, which allow for a few <a href="https://developers.google.com/maps/documentation/javascript/reference#PolylineOptions" rel="nofollow noreferrer">options</a> to be set, like for instance color and width.</li> <li>If you also would like to draw the rectangles with events bound to them (as in your example), you can use a <a href="https://developers.google.com/maps/documentation/javascript/reference#Rectangle" rel="nofollow noreferrer">Rectangle</a> with certain <a href="https://developers.google.com/maps/documentation/javascript/reference#RectangleOptions" rel="nofollow noreferrer">options</a>. You can bind 'click' events to these rectangles, such that you can interact with them. Or you could use the coordinates of the mouse click on the map to identify which square was clicked.</li> </ol> <p><strong>Extended information:</strong> If you know where to draw the grid, you also know where to draw the rectangles since the edges of the rectangles are basically line segments of the grid lines. So how do you know where to draw the grid lines? If you decide on a standard zero point (for instance the point where the equator and prime Meridian meet), and basically start drawing grid lines from there, you will always have the grid lines (and thus rectangles) positioned on the same location. Note, you only draw those grid lines which are within map's view of bounds. This way it is also fairly easy to identify a rectangle by for example it's top left corner...it will always be located on the same position.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload