我有一張實時使用傳單的地圖。傳單實時:SyntaxError:JSON.parse:意外字符
我像github上的描述一樣實時創建傳單。並與該示例中的URL效果很不錯,但是當我使用JSON字符串直接,我會收到以下錯誤消息:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data undefined leaflet-realtime.min.js:4:3695
的代碼如下所示:
var geojsonFeature = {"geometry": {"type": "Point", "coordinates": [-64.90913344523922, 31.274686201725011]}, "type": "Feature", "properties": {}};
var map = L.map('map',
{
center: [<?php echo $StartNorth; ?>, <?php echo $StartEast; ?>],
zoom: <?php echo $StartZoomLevel; ?>,
layers: [BasemapAT_basemap, geoJSONLayerNone],
zoomControl: false,
contextmenu: true,
contextmenuWidth: 200,
contextmenuItems: [{
text: 'Koordinaten anzeigen',
callback: showCoordinates
}, {
text: 'Karte hier zentrieren',
callback: centerMap
}, '-', {
text: 'Zoom in',
icon: 'assets/leaflet-contextmenu/dist/images/zoom-in.png',
callback: zoomIn
}, {
text: 'Zoom out',
icon: 'assets/leaflet-contextmenu/dist/images/zoom-out.png',
callback: zoomOut
}]
}
)
realtime = L.realtime({
geojsonFeature,
//url: 'https://wanderdrone.appspot.com/',
crossOrigin: true,
type: 'json'
}, {
interval: 3 * 1000,
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
'icon': L.icon({
iconUrl: 'leaflet/images/marker-icon-vehicle.png',
iconSize: [16, 16],
iconAnchor: [1, 8],
popupAnchor: [7, 0]
})
});
}}).addTo(map);
理論它應該工作,但我不能解釋自己爲什麼不能。 有人可以給我一個小費?
你'geojsonFeature'是一個對象,而不是你在你的問題的字符串。那是故意的嗎? – osdavison
實際上,從鏈接的頁面查看文檔,看起來好像不可能將JSON字符串用作參數「L.realtime」 – osdavison
是的,因爲我在其他示例中看到它類似(https:/ /github.com/perliedman/leaflet-realtime/issues/69)。 存儲在'geojsonFeature'中的字符串將在客戶端的間隔中自動生成。 – wuk986