Question

I have a problem. I want to show my markers in leaflet api but i can't. Because i can't create needed json format properly. This is what leaflet wants

var geojsonFeature = [{
            "type": "Feature",
            "properties": {
                "name": "Coors Field",
                "amenity": "Baseball Stadium",
                "popupContent": "This is where the Rockies play!"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [29.0502548217773, 41.0171458178711]
            }
        }, {
            "type": "Feature",
            "properties": {
                "name": "Coors Field",
                "amenity": "Baseball Stadium",
                "popupContent": "This is where the Rockies play!"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [29.1502548217773, 41.0171458178711]
                }
            }];

what i did

echo "[";

while ( $row = pg_fetch_row ( $result ) ) {
$marker_id = $row [0];
$name = $row [1];
$category = $row [2];
$information = $row [3];
$latitude = $row [4];
$longitude = $row [5];
$owner = $row [6];

// print ($marker_id . "," . $name . "," . $category . "," . $information . "," . $latitude . "," . $longitude . "," . $owner."\r\n") ;
$coordinates = "[" . $longitude . "," . $latitude . "]";

echo json_encode ( array (
        "type" => "Feature",
        "properties" => array (
                "name" => "Coors Field",
                "amenity" => "Baseball Stadium",
                "popupContent" => "This is where the Rockies play!" 
        ),
        "geometry" => array (
                "type" => "Point",
                "coordinates" => $coordinates
        ) 
) );
echo ",";
}

echo "]";

But in coordinates my json looks like coordinates":"[29.0505981445312,40.9878676184644]" but i dont want quotes. I want like this "coordinates": [29.0502548217773, 41.0171458178711] So how can i do that ?

Edit: i found solution $latitude = (float) $row [4]; $longitude = (float) $row [5]; $coordinates = array($longitude, $latitude); solve this problem. but stil not working because i think i need json object. My new code

echo "{type:\"FeatureCollection\",features:[";

$isfirst = true;
while ( $row = pg_fetch_row ( $result ) ) {
if ($isfirst){
    $isfirst = false;
}else{
    echo ",";
}
$marker_id = $row [0];
$name = $row [1];
$category = $row [2];
$information = $row [3];
$latitude = (float) $row [4];
$longitude = (float) $row [5];
$owner = $row [6];

// print ($marker_id . "," . $name . "," . $category . "," . $information . "," . $latitude . "," . $longitude . "," . $owner."\r\n") ;
//$coordinates = "[" . $longitude . "," . $latitude . "]";
$coordinates = array($longitude, $latitude);

echo json_encode ( array (
        "type" => "Feature",
        "properties" => array (
                "name" => "Coors Field",
                "amenity" => "Baseball Stadium",
                "popupContent" => "This is where the Rockies play!" 
        ),
        "geometry" => array (
                "type" => "Point",
                "coordinates" => $coordinates
        ) 
) );

echo "]}";

i take string and in js did this

var geoJSON = JSON.parse(data);
    L.geoJson(geoJSON).addTo(map);

but giving error SyntaxError: JSON.parse: expected property name or '}'

var geoJSON = JSON.parse(data);

Was it helpful?

Solution

i solved my geojson object problem. First i use eval method after that parse to json and now markers in map.

$.post( "show_markers.php")
.done(function(data) {

    var json = JSON.stringify(eval("(" + data + ")"));
    var obj = JSON.parse(json);

    L.geoJson(obj, {
        onEachFeature : function(feature, layer) {
            var popupContent = "<b>"+feature.properties.name+"</b><br>"+feature.options.information;
            if (feature.properties && feature.properties.popupContent) {
                //popupContent += feature.properties.popupContent;
            }
            layer.bindPopup(popupContent);
            layer.on('click', onClick);
            //layer.on('dragend', updateCoordinate);
        },
        pointToLayer : function(feature, latlng) {
            return new customMarker(new L.LatLng(feature.geometry.

            coordinates[1], feature.geometry.coordinates[0]), {
                marker_id : feature.options.marker_id,
                draggable : true
            });
        }
    }).addTo(map);

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top