0

Hello here is my code "drawing polygons" -how can you save the polygones in a JSON file? with click this polygon will change(deform). But if you do that, you save the new polygon into the JSON file that means: Read JSON file polygons-> Show polygon-> eg. Change name of zone or set new point-> Save in JSON-file

can someone help me please: i work with openlayer 5

     /// function draw Polygone( already workt)

     function DrawSnappingZone(MapIdentifier){                
     var coordinatearray = [];            
    var snappingpoints = prompt("Wieviele Punkte sollen gesetzt         werden?");                       
     MapIdentifier.on('click', function(evt)          
       {                  
        var single_coordinate = ol.proj.toLonLat(evt.coordinate);     
        coordinatearray.push(single_coordinate);           
        //console.log(coordinatearray);                      
        console.log(coordinatearray.length);                  
        //DrawSnappingZone(coordinatearray, 5);      
        if (coordinatearray.length == snappingpoints)                        
        {   
        coordinatearray.push(coordinatearray[0]);           
            RouteAnzeigenAusKoordinaten(coordinatearray,       KarteGlobal_o, 'red');         
        }        
    });
  //function route draw
  function RouteAnzeigenAusPolyLine(PolyLine_s, AufKarte_o, Farbe_s)
  {      
  // Dekodierung der Polyline, die von OSRM geliefert wurde.
  // -> in Koordinaten Array
  var Koordinaten_v = DecodePolyline(PolyLine_s);

  // DEBUG AUSGABE
    console.log(Koordinaten_v);

    // Array anzeigen
   RouteAnzeigenAusKoordinaten(Koordinaten_v, AufKarte_o, Farbe_s)
}



 // AufKarte_o: Openlayers-Objekt auf dem es angezeigt werden soll
 // Farbe_s: Farbe als String (red, blue, green oder #0000FF)
 function RouteAnzeigenAusKoordinaten(Koordinaten_v, AufKarte_o,    Farbe_s)
  {
  // Wandeln der Array-Einträge in einen geometry-String
   var polyline = new ol.geom.LineString(Koordinaten_v)

// Wandlung der Polylines in ein Referenzkoordinatensystem
polyline.transform('EPSG:4326', 'EPSG:3857');

// Wandeln der geometry-Polyline in ein Feature
var polyfeat = new ol.Feature(polyline);

var vectorLayer = new ol.layer.Vector(
{
    source : new ol.source.Vector(
    {
        features : [polyfeat]
    })

    ,

    style : new ol.style.Style(
    {
        fill : new ol.style.Fill(
        {
            color : 'black'
        }),
        stroke : new ol.style.Stroke(
        {
            color : Farbe_s,
            width : 3
        })
    })

});

  // Den Vektorlayer der Karte hinzufügen.
  AufKarte_o.addLayer(vectorLayer);    
      }


function DecodePolyline(str, precision)
{
 var index = 0,
    lat = 0,
    lng = 0,
    coordinates = [],
    shift = 0,
    result = 0,
    byte = null,
    latitude_change,
    longitude_change,
    factor = Math.pow(10, Number.isInteger(precision) ? precision : 5);

while (index < str.length) {

    // Reset shift, result, and byte
    byte = null;
    shift = 0;
    result = 0;

    do {
        byte = str.charCodeAt(index++) - 63;
        result |= (byte & 0x1f) << shift;
        shift += 5;
    } while (byte >= 0x20);

    latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

    shift = result = 0;

    do {
        byte = str.charCodeAt(index++) - 63;
        result |= (byte & 0x1f) << shift;
        shift += 5;
    } while (byte >= 0x20);

    longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

    lat += latitude_change;
    lng += longitude_change;

    coordinates.push([lng / factor, lat / factor]);
   }

   return coordinates;
   };
Remas1
  • 9
  • 2
  • Please decide if you want to hear about `openlayer` (I do not know), about `JSON.stringify()`, or about saving a file from JavaScript (which would make this question a duplicate of https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript). – tevemadar Jun 01 '19 at 14:38
  • I want to save the coordinate of the polygon in openlayer in a json file JSON.stringify() – Remas1 Jun 01 '19 at 14:50

0 Answers0