0

I have a field name: 'coordinates' I have the coordinates stored in there: like this: [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]

when I json_encode($geojson);

I get this output:

"coordinates": "[[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] "

notice the double quotes

it needs to be:

"coordinates": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]

How do I json_encode without those quotes for the coordinates?

Here is my full code.

while ($row = mysql_fetch_assoc($rs)) {
    $properties = $row;
    $geometry = $row;
    # Remove fields
    unset($properties['coordinates']);
    unset($properties['type']);
    unset($geometry['unitID']);
    unset($geometry['milesSq']);
    unset($geometry['acres']);
    $feature = array(
        'type' => 'Feature',
        'properties' => $properties,
        'geometry' => array($geometry)        
    );
    # Add feature arrays to feature collection array
    array_push($geojson['features'], $feature);
}

echo json_encode($geojson);

results:

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"unitID":"001","milesSq":"111.000000","acres":"222.000000"},"geometry":[{"type":"Polygon","coordinates":"[[[-109.048595341028587, 40.834756771179649], [-108.966734749488324, 40.562222787215255],  [-109.048595341028587, 40.834756771179649]]]"}]}]}

ON JS:

$.ajax({
            type: "post", 
            url: "<?php echo $this->app_lib->FOLDER['admin'].$this->router->class ?>/buscador", 
            dataType: "json", 
            data: {clat:center.lat, clng:center.lng, zoom:zoo, minlat:min.lat, minlng:min.lng, maxlat:max.lat, maxlng:max.lng, layers:<?php echo json_encode($layers); ?>}, 
            success: function(result){
              //if (data.error) {
                //map.removeLayer(data.points); 
                $("#loader").hide()
                $.each(result, function(index, layer) {
                    if(layer.data.layer == 3){
                            $.each(layer.data.features, function(index, polygons) {
                                console.log(polygons.geometry.coordinates);
                                var poly = L.geoJson(campus, {
                                style: function (feature) {
                                    return feature.properties && feature.properties.style;
                                },
                                onEachFeature: onEachFeature,
                            });
                            map.addLayer(poly); 
});
  • What is the data type of the coordinates column? It looks like text in your question. As far as I know there is no array data type in MySQL – toms Feb 6 '15 at 16:12
  • you're storing the coordinate data as a text field in the database? – tomfumb Feb 6 '15 at 16:48
  • Yes.. I have a longtext field and placing the polygon in this following format [[-3.96881103515625, 40.56510079313928],[-4.0045166015625, 40.49829983826093],[-3.856201171875, 40.49934411527887]] – Nataliya Feb 9 '15 at 11:13
3

In the javascript code the array is treated as a string so try to parse this sting as json before using , then assign the result to coordinates . So if your result in success function is result , parse its coordinates attribute before using it this is example how to do so :

      result = {
     "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {
            "unitID": "001",
            "milesSq": "111.000000", "acres": "222.000000"
        },
        "geometry": [
            {
                "type": "Polygon",
                "coordinates": "[[[-109.048595341028587, 40.834756771179649], [-108.966734749488324, 40.562222787215255],  [-109.048595341028587, 40.834756771179649]]]"
            }
          ]
        }
       ]
     }


$.each(result.features, function (index, polygons) {
    $.each(polygons.geometry, function (index, geo) {
    console.log(geo.coordinates);
    var coordinates = JSON.parse(geo.coordinates);
    geo.coordinates = coordinates;


})
})


console.log(result)

  ///////  here u can start using the result object
  ///////   the leaflet code goes here 

OR in the php code you can decode the stringified array before passing it like this :

 $geometry['coordinates']=json_decode($geometry['coordinates']);

Hope this helps.

  • But I am passing directly to the JSON Objects into L.geoJson function. var poly = L.geoJson(campus, {style: function (feature) { return feature.properties && feature.properties.style; },onEachFeature: onEachFeature,});map.addLayer(poly); – Nataliya Feb 9 '15 at 11:14
  • First thing you should do in the success function is to iterate on the result object returned by php and parse the coordinates . I will edit answer to give example – Boudi Feb 10 '15 at 5:26
  • I'd strongly recommend using the second approach - decoding the coordinates in PHP using the extra single line. The result has the benefit of being a valid GeoJSON, no client-side hacks in JS required. – Jan Šimbera Feb 10 '15 at 9:00
  • I prefer that too ;), we should try to keep the client side clean and let the server handle the data processing. – Boudi Feb 10 '15 at 22:26
0

It works but abit tricky way...

$.each(layer.data.features, function(index, polygons) { var coordinates = JSON.parse(polygons.geometry.coordinates); var values = []; var coord = []; for(var i = 0, l = coordinates.length; i < l; i++) { values.push(coordinates[i]); }
coord.push(values); var campus = { 'type': 'Feature', 'properties': { 'name': polygons.properties.name, 'category': polygons.properties.category, 'description': polygons.properties.description, 'style': { weight: 2, color: polygons.properties.style.color, opacity: 1, fillColor: polygons.properties.style.color, fillOpacity: 0.4 }}, 'geometry': { 'type': 'Polygon', 'coordinates': coord } };

                            var poly  = L.geoJson(campus,  {
                                                        style: function (feature) {
                                                            return feature.properties && feature.properties.style;
                                                        },
                                                        onEachFeature: onEachPolygonFeature,

                            });
                            map.addLayer(poly); 

                        });

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.