I want to write the name of the polygon in a form and then click a button to zoom or to flyto to the centroid of the polygon. I've writen a request in PHP that give the centroid of the polygon.

function localiser5() {
  var val6 = document.getElementById('val6').value;
  var x;
  var y;
  var z;
      type: 'POST',
      url: 'php/survoler.php',

      data: {
        val6 : val6

     map.flyTo(new L.LatLng(x, y),12);  



$db = pg_connect("host=localhost port=5432 dbname=d3topo user=postgres password=khadija") or die('could not connect :'.pg_last_error());

  $champ_geometrique = 'geom';
  $val6 = $_POST['val6'];

  $sql = "SELECT st_x(st_centroid(geom)) as x, st_y(st_centroid(geom)) as y
          FROM plan_situation where nom_plan='".$val6."'";

  $rs = pg_query($db, $sql);
  if (!$rs) {
    echo "Erreur SQL survenue!.\n";
  $testRow = pg_fetch_row($rs);
  echo $testRow[0];



<input  type="text" placeholder="Nom plan" name="val6" id="val6" />  
<button type="submit" onClick="localiser5()" >FlyTo Polygon </button>
  • Is there a question hidden in there somewhere? – lynxlynxlynx Mar 14 '18 at 14:23
  • where exactly ? – Mnl Mar 15 '18 at 11:20
  • 1
    where, indeed...we do need a question ,) where is your problem? be specific. – ThingumaBob Mar 15 '18 at 12:19

It would be better to select the polygon and zoom to it. The way this is worded you could have an array of objects name, X, Y, and just use the setView with a constant zoom set. Problem with this is if each polygon is a different size you may not see all of the polygon. You would also have to capture different zoom scales to show each polygon.

For a class I used a simple JQuery Autocomplete input and wrote up how to zoom and highlight a "selected" polygon. From this page I have my code and a working example.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.