-1

I refer to the question Polygon "Drawing and Getting Coordinates with Google Map API v3" and the code by jhawes which works fine; BUT I've struggle to post the lat/lng-values to a DB using php. In other scripts with single coordinates I use the following (whereas the variables "BlattNr, Quadrant, MTBlat, MTBlng, Qmlat, Qmlng" are of no interest for that question):

function saveData(BlattNr, Quadrant, MTBlat, MTBlng, Qmlat, Qmlng) {

var latlng = marker.getPosition();

  window.location.href = "schritt_2.php?lat=" + latlng.lat() + 
      "&lng=" + latlng.lng() + "&MTBNr=" + BlattNr + "&Quadrant=" + Quadrant + "&MTBlat=" + MTBlat + "&MTBlng=" + MTBlng + "&Qmlat=" + Qmlat + "&Qmlng=" + Qmlng;
  marker.setMap(null);
  }

With this I can post the lat and lng of the single point coordinate - but how to pass the multiple lat and lng of various points? I don't know how to define the variable and how to construct the "saveData" function... Many thanks in advance for any help :-)

Community
  • 1
  • 1
Randy
  • 31
  • 1
  • 8

1 Answers1

0

Okay, I copy/pasted the code that jhawes put in jsFiddle

I added a few things.
add jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Save_data

function save_data() {
  var data = [];
  var len = myPolygon.getPath().getLength();
  for (var i = 0; i < len; i++) {
    data.push([myPolygon.getPath().getAt(i).lat(), myPolygon.getPath().getAt(i).lng()]);
  }
  // send data to the server
  $.ajax({
    url: 'save.php?p=' + JSON.stringify(data), 
    success: function (message) {
      $('#ajaxresponse').html(message);
    }
  });
}

A button and a display div. Put them somewhere at the bottom of the markup.

<input type="button" onclick="save_data()" value="SAVE">
<div id="ajaxresponse"></div>

Server side: I don't do much here; I'm sure you can handle it.

save.php

<?php
if (isset($_GET['p'])) {
  $locations = json_decode($_GET['p'], true);
  echo print_r($locations, true) . '<br>';
  echo "let's print the second point: lat=" . $locations[1][0] . ", lng=" . $locations[1][0];
}
?>
Emmanuel Delay
  • 3,619
  • 1
  • 11
  • 17