我有问题。我想在传单api中显示我的标记,但我不能。因为我无法正确创建所需的json格式。 这是传单想要的
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]
}
}];
我做了什么
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 "]";
但是在坐标中我的json看起来像坐标“:”[29.0505981445312,40.9878676184644]“但我不想要引号。我想要这样的”坐标“:[29.0502548217773,41.0171458178711]那我怎么能这样做?
编辑:我发现解决方案$latitude = (float) $row [4]; $longitude = (float) $row [5]; $coordinates = array($longitude, $latitude);
解决了这个问题。但由于我认为我需要json对象,所以不能正常工作。
我的新代码
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 "]}";
我拿字符串并在js中做了这个
var geoJSON = JSON.parse(data);
L.geoJson(geoJSON).addTo(map);
但是给出错误SyntaxError:JSON.parse:期望的属性名称或'}'
var geoJSON = JSON.parse(data);
答案 0 :(得分:0)
我解决了我的geojson对象问题。首先,我使用eval方法解析为json,现在是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);
});