Google MAPS API V3 & # 8594; 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 helping out. I draw polygons on my website and everything works fine. I save the coordinates of my polygons in the MYSQL database, and here is the table structure:

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 split lats and longs into separate columns and i get the coordinates using php ..

The lat_localite column contains only the latitude coordinates, and the long_localite column contains only the longitude coordinates, I get these coordinates using Javascript and writing the contents into 2 separate text fields .. 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; } }); 

Now I want to display all the polygons that are stored in my table, and I do not know how to do this. Can you help me?

+4
source share
1 answer

With V3, it's pretty easy.

 var polygons = []; 

And then just skip all your polingons from the 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 (see Polygon Data Type )

This may help if you need to request data. You are limited to minimal bounding boxes, but I think this data type is better.

To extract the polygon from MySql as lat / lng coordinates, see mysql AsText () function.

eg:

 select AsText(my_polygon_column) as blob; 

And then pass this 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; } 
+2
source

Source: https://habr.com/ru/post/1410911/


All Articles