0

Being a programming novice, I have been stuck on this for a few days. All code below I have researched from this site and others to solve the problem with no success.

I can successfully load data onto Google Maps from a json file on my server with:

map.data.loadGeoJson('http://www.h...........com/geojson/164_surveydata.json');
map.data.setStyle({
      editable: false,
      draggable: false,
      strokeWeight: 2,
      strokeColor: '#686868',
    });

I can then edit the polylines, moving nodes to new positions.

I then attempt to save the edited map data layer with a panel button onclick function:

function saveData() {

map.data.setStyle({
  editable: false,
  strokeWeight: 2,
  strokeColor: '#686868',
  });

 map.data.toGeoJson(function(data) {
   var jsonData = JSON.Stringify(data);
  });

 $.ajax({
    method: 'POST',
url: 'http://www.h..........com/savegeojson.php',
data: {'data' : jsonData},
success: function(response){
             alert( response );
    },
    error: function (response) {
             alert('error'+ response);
             }
});
}

I use the same php requirements that successfully posts data to a SQL database on my Server, thus assuming i can do the same to access files on the same server. My php file is as follows:

<?php
 require __DIR__ . "/getdb.php";

  if (!mysql_connect($db_host, $db_user, $db_pwd))
   die("Can't connect to database");

 if (!mysql_select_db($database))
     die("Can't select database");

  $json = json_decode($_POST['data'],true);

     if (json_decode($json) != null) { /* sanity check */
 $file = fopen('http://www.h.....com/geojson/164_surveydata.json','w+');
     fwrite($file, $json);
     fclose($file);
    } else {
     // handle error 
    }
  ?>

The idea is to load the json file into google maps, edit polylines etc, and then save the data back to the same json file.

Any help is greatly appreciated. Also any tips as to where to place errorhandlers(?) will also be awesome.

1 Answers1

2

To simplify things, just remove your json file and put it in the database. Use your php code to create a json format string that holds the coordinates of each polylines.  Here is a sample for editable nodes of polylines. For saving the polyline path kindly refer to this SO question. As for error handling you can put this upon getting the coordinates from the database, for plotting the polylines in the map and lastly is saving the coordinates in the DB. 

Here is the reason in doing this:

  • saving it to a json will be redundant, because many factors have to consider when overwriting a file to the server. To simply it, save the coordinates to the database.

  • the file you will upload to the server will be lessen.

  • file dependency.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Thanks KENdy, your suggestion has prompted a rethink to solve the problem and improve performance. I now treat each polyline individually and save to the database using XMLHttp, with each polyline's coordinates encoded. So I am now not converting to Json format, just a text column in the database for the encoded polyline. – user5997645 Mar 07 '16 at 19:42