将Openlayers 3和proj4js与RequireJS一起使用时遇到问题。
使用标准的JavaScript文件和html,我有可用的代码来显示地图,并使用Openlayers鼠标位置控件显示EPSG:27700中的坐标。
当我使用RequireJS(请参见下面的代码)时,由于proj.get返回“ undefined”,代码失败。Chrome中的错误提示Require引发了错误。
我曾尝试使用垫片,但这没有用,我也不相信这是正确的方法。谁能告诉我如何使它工作?
require.config({
baseUrl: './',
paths: {
'domReady': '../lib/domReady',
'openlayers': '../lib/ol',
'proj4': '../lib/proj4'
}
});
require([
'domReady',
'openlayers',
'proj4'
], function (domReady, openLayers, proj4) {
"use strict";
function getLayers() {
var baseLayer = new openLayers.layer.Tile({
source: new openLayers.source.OSM()
});
return [baseLayer];
};
domReady(function () {
proj4.defs('EPSG:27700', '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs');
openLayers.proj.get("EPSG:27700").setExtent([0, 0, 700000, 1300000]);
var mousePositionControl = new openLayers.control.MousePosition({
coordinateFormat: openLayers.coordinate.createStringXY(4),
projection: 'EPSG:27700'
});
var map = new openLayers.Map({
target: 'map',
layers: getLayers(),
controls: [mousePositionControl],
view: new openLayers.View({
projection: 'EPSG:27700',
center: [300000, 500000],
resolutions: [4500, 3200, 2400, 1600, 800, 400, 200, 100, 50, 25, 10, 5, 2.5, 1, 0.5, 0.25, 0.125, 0.0625],
zoom: 3,
minResolution: 25,
maxResolution: 800
})
});
});
});
您必须先告诉OpenLayers如何找到proj4.js:
openLayers.proj.setProj4(proj4);
上面的代码片段要求OpenLayers> = v3.13.0,并假设设置类似于问题中的代码片段。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句