Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a static 1km grid to google maps using php

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/

like image 703
redshark1802 Avatar asked Apr 28 '12 14:04

redshark1802


People also ask

How can I add Google map in my website using PHP?

Get a Google Map API key. We will create a div that will display the map. We will create API wrapper in PHP that will get formatted address, latitude and longitude using the City name. We will create an Ajax request that will call the above PHP API and pass data to the google map instance.

What is LatLng in Google Map?

A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.

How do you make a grid on Google Earth?

The easiest way to show the coordinate grids in Google Earth is to select “Grids” from the “Tools” options. Then you will have full zoomable coordinates grid cover (Pic 1 – 3).


3 Answers

The answer to your question can be found in the Google Maps API (v3).

The basic approach here is:

  1. 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.
  2. 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.
  3. Draw the grid in the shape of PolyLines, which allow for a few options to be set, like for instance color and width.
  4. 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.

like image 50
dennisg Avatar answered Sep 17 '22 19:09

dennisg


maybe these examples will help: this is a fixed size grid - position it with the NW latlng and size it using the height and width variables

this is a grid that resizes and moves to cover the map area (more or less)

both of them store the rectangles in the rectArr array, so you can manipulate their options, etc, by accessing that.

like image 31
lucas Avatar answered Sep 17 '22 19:09

lucas


You want a quadkey. Geohash uses a similar system. You can look for a L-system to write a z curve or you can grab my code at phpclasses.org (hilbert curve). Here is good tutorial on how it works: http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves.

like image 37
Micromega Avatar answered Sep 17 '22 19:09

Micromega