adding a static 1km grid to google maps using php
api, google-maps, javascript, php
Solution
The answer to your question can be found in the Google Maps API (v3).
The basic approach here is:
- Find the bounds of the map using the getBounds() method of the Map object. The result is a LatLngBounds object, from which you can extract the latitude and longitude coordinates of the corners of the map.
- Compute the distance 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.
- Draw the grid in the shape of PolyLines, which allow for a few options to be set, like for instance color and width.
- If you also would like to draw the rectangles with events bound to them (as in your example), you can use a Rectangle with certain options. 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.
Extended information: 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.
Problem
I want to create a grid that I'll be overlaying google maps. The grid has to be static, meaning 1km² grid has to be exactly at the same location and has to be identifiable with a unique id. How can I achieve this in google maps and php? The best, redshark1802 edit: Forgot to mention that I have to interact with these grids direclty, meaning changing color/style for each field. I've found some site that did it already https://ownthisworld.com/