呈现Geojson非常不稳定

呈现Geojson非常不稳定,json,openlayers-3,Json,Openlayers 3,从Openlayers-2.11迁移到ol3时,我遇到了一个奇怪的问题。从mysql数据库动态提取的geojson文件有时呈现,有时不呈现。提取是通过PHP脚本完成的。请举一个活生生的例子。 我已经通过几个在线json验证器运行了该文件,但结果都是“有效的json”。由于geojson文件是动态的,我的第一个想法是文件中可能隐藏了一些非法字符,但我找不到任何字符。ol3的实现是直接的 var image = new ol.style.Circle({ radius: 5, fill: n

从Openlayers-2.11迁移到ol3时,我遇到了一个奇怪的问题。从mysql数据库动态提取的geojson文件有时呈现,有时不呈现。提取是通过PHP脚本完成的。请举一个活生生的例子。 我已经通过几个在线json验证器运行了该文件,但结果都是“有效的json”。由于geojson文件是动态的,我的第一个想法是文件中可能隐藏了一些非法字符,但我找不到任何字符。ol3的实现是直接的

var image = new ol.style.Circle({
  radius: 5,
  fill: null,
  stroke: new ol.style.Stroke({color: 'red', width: 1})
});

var styles = {
 'Point': [new ol.style.Style({image: image})]
};

var styleFunction = function(feature, resolution) {
  return styles[feature.getGeometry().getType()];
};  

var vectorLayer = new ol.layer.Vector({
    source: new ol.source.GeoJSON({
                url: '../../../../yg/utils/retriveData.php',
                projection: 'EPSG:3857'
            }),
    style: styleFunction
});
我已经通过相同的代码运行了其他静态json文件,没有任何麻烦。问题似乎仅限于此动态文件

请允许我询问关于在这种情况下什么是最佳调试策略的指导原则

PHP脚本如下所示

<?php
$conn = new mysqli("localhost", "xxxxxxxx", "1234", "xxxxx");

$sql = 'SET names utf8';

$rs = $conn->query($sql);

if (!$rs) {
  echo 'An SQL error occured.\n';
  exit;
}

$sql = 'SELECT * FROM extract_data';

# Try query or error
$rs = $conn->query($sql);
if (!$rs) {
   echo 'An SQL error occured.\n';
exit;
}

# Build GeoJSON feature collection array
$geojson = array(
   'type'      => 'FeatureCollection',
   'features'  => array()
);

# Loop through rows to build feature arrays
while ($row = $rs->fetch_array(MYSQLI_ASSOC)) {
   $properties = $row;
   # Remove x and y fields from properties (optional)
   unset($properties['lat']);
   unset($properties['lon']);
   $feature = array(
    'type' => 'Feature',
    'geometry' => array(
        'type' => 'Point',
        'coordinates' => array(
            $row['lon'],
            $row['lat']
        )
    ),
    'properties' => $properties
);
# Add feature arrays to feature collection array
array_push($geojson['features'], $feature);
}

header('Content-type: application/json');

echo json_encode($geojson, JSON_NUMERIC_CHECK);

$conn = NULL;
?>

可能与您的问题无关,但我发现您的代码中有错误

您没有正确调用
样式函数
,也没有传递其
功能
分辨率
参数

您可以尝试以下方法进行调试:

var styleFunction = function(feature, resolution) {
  return styles['Point']; //for debugging
};  

var vectorLayer = new ol.layer.Vector({
    source: new ol.source.GeoJSON({
                url: '../../../../yg/utils/retriveData.php',
                projection: 'EPSG:3857'
            }),
    style: styleFunction()
});
另外,从中,您应该从geojson创建向量层,如下所示:

var vectorLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
                url: '../../../../yg/utils/retriveData.php',
                format: new ol.format.GeoJSON(),
                projection: 'EPSG:3857'
            }),
    style: styleFunction()
});

可能与您的问题无关,但我发现您的代码中有错误

您没有正确调用
样式函数
,也没有传递其
功能
分辨率
参数

您可以尝试以下方法进行调试:

var styleFunction = function(feature, resolution) {
  return styles['Point']; //for debugging
};  

var vectorLayer = new ol.layer.Vector({
    source: new ol.source.GeoJSON({
                url: '../../../../yg/utils/retriveData.php',
                projection: 'EPSG:3857'
            }),
    style: styleFunction()
});
另外,从中,您应该从geojson创建向量层,如下所示:

var vectorLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
                url: '../../../../yg/utils/retriveData.php',
                format: new ol.format.GeoJSON(),
                projection: 'EPSG:3857'
            }),
    style: styleFunction()
});

可能是异步问题。你有没有注意到,如果你的json越大,它就越不呈现?请将你的php限制在10个功能中,以便于调试。你似乎在使用旧版本的ol3。您使用的是哪个版本?@Alvin Lindstam-我使用的是v3.4.0,因为我遵循了《OpenLayers 3之书》(the book of OpenLayers 3)中的指导原则(@stenh ol3)不断发展,最新版本是3.10.1(并且ol.source.GeoJSON已被删除)。如果可能,我建议使用更高版本。可能是异步问题。你有没有注意到,如果你的json越大,它就越不呈现?请将你的php限制在10个功能中,以便于调试。你似乎在使用旧版本的ol3。您使用的是哪个版本?@Alvin Lindstam-我使用的是v3.4.0,因为我遵循了《OpenLayers 3之书》(the book of OpenLayers 3)中的指导原则(@stenh ol3)不断发展,最新版本是3.10.1(并且ol.source.GeoJSON已被删除)。如果可能,我建议使用更高版本。