简体   繁体   English

从MySQL获取数据到GeoJSON时LeafletJS不显示地图

[英]LeafletJS not showing the map when getting data to GeoJSON from MySQL

So I can get the data from MySQL with this Php query 所以我可以通过这个PHP查询从MySQL获取数据

while($row = mysql_fetch_assoc($dbquery)) {
$feature = array(
'type' => 'Feature', 
'geometry' => array(
'type' => 'Point',
'coordinates' => array((float)$row['lng'], (float)$row['lat'])
),
'properties' => array(
'pano' => $row['pano']
//Other fields here, end without a comma
)
);
array_push($geojson['features'], $feature);

And when echoing the array with json_encode($geojson,JSON_NUMERIC_CHECK); 并且当用json_encode($ geojson,JSON_NUMERIC_CHECK)回显数组时; the result is 结果是

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[25.726231,66.498966]},"properties":{"pano":"81_1_0.html"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[25.723139,66.50158]},"properties":{"pano":"81_1_0.html"}}]};

But when I'm trying to get those into the LeafletJS map nothing shows up 但是,当我尝试将它们放入LeafletJS地图时,什么都没有显示

var points = <?php echo json_encode($geojson, JSON_NUMERIC_CHECK); ?> ;
var geoJsonLayer = L.geoJson(points, {
    pointToLayer: function (feature, latlng) {
        var marker = L.circleMarker(latlng, geojsonMarkerOptions);

        marker.on('click', function (e) {
            var feature = e.target.feature;
            var content = '<iframe width="665" height="650" frameborder="0" src="/' + feature.properties.pano + '">';
            document.getElementById("event").innerHTML = content;

            if (selectedMarker != false) {
                selectedMarker.setStyle({
                    fillColor: "#ff7800"
                });
            }
            marker.setStyle({
                fillColor: "#000000"
            });
            selectedMarker = marker;
        });

        return marker;
    }
});
markers.addLayer(geoJsonLayer);

What I'm doing wrong here? 我在这里做错了什么?

Try below :- 请尝试以下:-

var bicycleRental = {

  "type": "FeatureCollection",
  "features":[{
      "type": "Feature",
      "properties": {
        "point_id": "m14885"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          101.52053833007812,
          3.065117257466733
        ]
      }
    }


L.geoJson(bicycleRental, {

                style: function (feature) {
                    return feature.properties && feature.properties.style;
                },

                onEachFeature: onEachFeature,

                pointToLayer: function (feature, latlng) {
                    return L.circleMarker(latlng, {
                        radius: 1.5,
                        fillColor: "#ffffff",
                        color: "#000",
                        weight: 0.8,
                        opacity: 0.5,
                        fillOpacity: 0.5
                    });
                }
            }).addTo(map);

shouldn't you be doing something like this 你不应该这样做吗

var points = JSON.parse(<?php echo json_encode($geojson, JSON_NUMERIC_CHECK); ?>);

otherwise points will just be a string. 否则, points只是一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM