当应用程序启动时,我想将地图视图放在用户当前位置的中心。我尝试了两种不同的方法,但无法让它们发挥作用。第一个在传单中正常工作,但在开发过程中我决定改用OL3。
第一种方法(在传单中工作):
var myProjectionName = "EPSG:25832";
proj4.defs(myProjectionName,
"+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs");
var centerPosition;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (pos) {
centerPosition =
ol.proj.transform(
[position.coords.longitude,
position.coords.latitude],
'EPSG:4326',
myProjectionName);
},
function (err) {centerPosition = [724844,6178000];},
{
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 1000
});
}
我的第二种方法是使用 ol.Geolocation 类:
var proj1 = ol.proj.get(myProjectionName);
var geolocation = new ol.Geolocation({
projection: proj1
});
var centerPosition= geolocation.getPosition();
中心位置用于创建视图/地图对象:
var map = new ol.Map({
target: 'map',
logo : false,
layers: [ GSTGroup, OVLGroup, SheatLayer],
view: new ol.View({
projection: myProjectionName,
center: centerPosition,
resolutions : AVLresolutions,
resolution : 2
})
});
我怀疑问题的原因是投影,但另一方面,投影在转换图层(WMTS,矢量)时正常工作,来自不同坐标系和 ol.control.MousePosition 中的 Geojson 的源。
我正在使用 Firefox 32.0.3 和 geolocator 插件进行开发/测试