无法让亲戚 URL 在 OpenLayers3 中工作
Can't get relative URL's working in OpenLayers3
三天来到处(学习书籍OpenLayers3,Javascript和互联网)寻找解决方案后,我把我的问题放在这里。
问题是我无法让 url 的亲戚在 OpenLayers3 中工作。这里我举个例子:
我在一个名为沙箱的 map/directory 中有一个 OpenLayersscript。
此脚本的 HTML 部分中的相对 url 正在工作,包括 javascript 到 ol.js 中的相对 url。
问题是脚本 Javascript 部分中的相关 url 不起作用。只有当目标文件 (nutsv9_lea.geojson) 位于包含 OpenLayersscript 本身的 map/directory 底层的 map/directory 中时,它才有效,但仅在 Firefox 中而不在 Google [=46= 中] 和 InternetExplorer。
地图沙箱(包含此 OpenLayers 文件)位于 maps/directories 结构中:C:/ol3_samples/sandbox
目标文件 (nutsv9_lea.geojson) 位于 maps/directories 结构中:C:/ol3_samples/assets/data/nutsv9_lea.geojson
我用的相对url是:url: '../assets/data/nutsv9_lea.geojson'
唯一可行的解决方案(如上所述,仅在 Firefox 中)是相对 url 定位到名为 'data' 的底层 map/directory,其中包含目标文件:url : map/directory结构中的'data/nutsv9_lea.geojson':C:/ol3_samples/sandbox/data/nutsv9_lea.geojson
我做错了什么或者我忽略了什么?
<script>
var vectorSource = new ol.source.GeoJSON({
projection: 'EPSG:3857',
//not working relative url:
// url: '../assets/data/nutsv9_lea.geojson'
//working url (with the targetfile in a directory below the directory containing this script) but only working in Firefox and not working in Chrome and InternetExplorer
url: 'data/nutsv9_lea.geojson'
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var center = ol.proj.transform([5.231819, 52.091852], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: center,
zoom: 5
});
var map = new ol.Map({
target: 'map',
layers: [vectorLayer],
view: view
});
</script>
我是这本书的作者之一,该摘录来自该书。您需要 运行 本地服务器(通过 http:// 而不是 file:// 提供文件)。
你只需要看看第 117 页。
它告诉你可以运行(只要你有Python)
python -m SimpleHTTPServer
或者如果您从 Packt 网站获得样本,运行(如果您有节点)
node index.js
对于 NodeJS 和 Python,您有安装说明(第 417 和 418 页),
对于 Python 和没有这本书的人,请转到 http://docs.python-guide.org/en/latest/starting/installation/
三天来到处(学习书籍OpenLayers3,Javascript和互联网)寻找解决方案后,我把我的问题放在这里。
问题是我无法让 url 的亲戚在 OpenLayers3 中工作。这里我举个例子:
我在一个名为沙箱的 map/directory 中有一个 OpenLayersscript。 此脚本的 HTML 部分中的相对 url 正在工作,包括 javascript 到 ol.js 中的相对 url。
问题是脚本 Javascript 部分中的相关 url 不起作用。只有当目标文件 (nutsv9_lea.geojson) 位于包含 OpenLayersscript 本身的 map/directory 底层的 map/directory 中时,它才有效,但仅在 Firefox 中而不在 Google [=46= 中] 和 InternetExplorer。
地图沙箱(包含此 OpenLayers 文件)位于 maps/directories 结构中:C:/ol3_samples/sandbox 目标文件 (nutsv9_lea.geojson) 位于 maps/directories 结构中:C:/ol3_samples/assets/data/nutsv9_lea.geojson
我用的相对url是:url: '../assets/data/nutsv9_lea.geojson'
唯一可行的解决方案(如上所述,仅在 Firefox 中)是相对 url 定位到名为 'data' 的底层 map/directory,其中包含目标文件:url : map/directory结构中的'data/nutsv9_lea.geojson':C:/ol3_samples/sandbox/data/nutsv9_lea.geojson
我做错了什么或者我忽略了什么?
<script>
var vectorSource = new ol.source.GeoJSON({
projection: 'EPSG:3857',
//not working relative url:
// url: '../assets/data/nutsv9_lea.geojson'
//working url (with the targetfile in a directory below the directory containing this script) but only working in Firefox and not working in Chrome and InternetExplorer
url: 'data/nutsv9_lea.geojson'
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var center = ol.proj.transform([5.231819, 52.091852], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: center,
zoom: 5
});
var map = new ol.Map({
target: 'map',
layers: [vectorLayer],
view: view
});
</script>
我是这本书的作者之一,该摘录来自该书。您需要 运行 本地服务器(通过 http:// 而不是 file:// 提供文件)。
你只需要看看第 117 页。
它告诉你可以运行(只要你有Python)
python -m SimpleHTTPServer
或者如果您从 Packt 网站获得样本,运行(如果您有节点)
node index.js
对于 NodeJS 和 Python,您有安装说明(第 417 和 418 页),
对于 Python 和没有这本书的人,请转到 http://docs.python-guide.org/en/latest/starting/installation/