Samouray
Samouray

Reputation: 39

Google MAPS API V3 --> How to display all the Polygons that are stored in my MYSQL table?

I'm pretty new on google MAPS API V3 and I like to get some help. I'm drawing polygons on my site and it works fine. I save the coordinates of my Polygons on a MYSQL database, and here is the structure of the table:

CREATE TABLE IF NOT EXISTS `localite` (
  `id_localite` int(11) NOT NULL AUTO_INCREMENT,
  `libele_localite` varchar(255) NOT NULL,
  `lat_localite` text NOT NULL,
  `long_localite` text NOT NULL,
  PRIMARY KEY (`id_localite`)
)

as you can see I have devided the lats and longs into seperate columns and I get the coordinates using PHP ..

the lat_localite Column contains just the Latitude coordinates, and the long_localite Column contains just the longitude coordinates , I get this coordinates by using Javascript and writing the content in 2 seperate textareas .. this is the code:

google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
  var patths = polygon.getPath();
  var stuh = patths.getLength();
    var lat_localite='';
    var long_localite='';
        for (var i=0; i<(stuh) ;i++)
        {
        lat_localite  += (patths.getAt(i).lat()+'\n');
        long_localite += (patths.getAt(i).lng()+'\n');

        document.forms['formId'].elements['Textarea1ID'].value=lat_localite;
        document.forms['formId'].elements['Textarea2ID'].value=long_localite;

        }   
});

SO now I Want to display all the Polygons that are stored in my table AND i don't know How to do it Can You Help me ?

Upvotes: 4

Views: 4887

Answers (1)

Niall
Niall

Reputation: 435

With V3 it's quite easy.

var polygons = [];

And then just loop through all your polyongs from database with the following code.

  //Add all your coordinates in with new google.maps.LatLng(lat, lng),
  var coords = [
    new google.maps.LatLng(12.5, -8.2),
    new google.maps.LatLng(12.52, -8.21),
    etc....
  ];

  // Construct the polygon
  polygons.push(new google.maps.Polygon({
    paths: coords,
    other_options: etc....
  }));

  polygons[polygons.length-1].setMap(map); //Your map object

Secondly (out of interest), MySql can store Polygons using Spatial Data Types (look at Polygon data type)

This can help if you need to query the data. You are restriected to minimum bounding rectangles but I think this data type is better for .

To extract the polygon from MySql as lat/lng coordinates look at mysql's AsText() function.

eg:

select AsText(my_polygon_column) as blob;

And then pass that through this php function:

function sql_to_coordinates($blob)
    {
        $blob = str_replace("))", "", str_replace("POLYGON((", "", $blob));
        $coords = explode(",", $blob);
        $coordinates = array();
        foreach($coords as $coord)
        {
            $coord_split = explode(" ", $coord);
            $coordinates[]=array("lat"=>$coord_split[0], "lng"=>$coord_split[1]);
        }
        return $coordinates;
    }

Upvotes: 3

Related Questions