adding a static 1km grid to google maps using php

13,563

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
13,563
redshark1802
Author by

redshark1802

Updated on June 14, 2022

Comments

  • redshark1802
    redshark1802 almost 2 years

    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/

  • redshark1802
    redshark1802 about 12 years
    Thanks so far, but how can I make sure that the Rectangles I draw will be at the same location every time?
  • dennisg
    dennisg about 12 years
    Added some extra information for you. Basic idea: decide on a standard zero point.
  • dennisg
    dennisg about 12 years
    @Chibox Correct, OP needs the LatLngBoundsObject of the viewport such that he can draw the grid overlay within the viewport. Everytime the user pans or zooms the map, the overlay needs to be redrawn. I don't see how that poses a problem?
  • redshark1802
    redshark1802 about 12 years
    @dennisg I know where you're going, but I would have to precompute all Grids. I know it sounds simple, but how exactly would I do it? Aren't there already things like a UTM Grid?
  • redshark1802
    redshark1802 about 12 years
    @Chibox I want to use GoogleMaps, and yes I basically had the same idea as dennisg but it is really to complicated.
  • redshark1802
    redshark1802 about 12 years
    I will look into it and share the results of my findings. Thanks.
  • redshark1802
    redshark1802 about 12 years
    Well, I couldn't figure it out. Sorry, but it's a bit to abstract. Could someone please provide some real example.
  • bukowski
    bukowski over 11 years
    I'll have to try this. This approach could potentially solve the performance issues too if drawing grid that fits in viewport... of course if you are not zoomed out too much.
  • Badams
    Badams almost 10 years
    @fjckls & redshark1802, were either of you ever able to get this going? I realize the question is old, but I am looking to do something similar for a while and have been struggling as well.