Javascript Openlayers 6地理位置控制
我正在重新设计地理位置控制功能,但我不知道我做错了什么。我收到错误消息:UncaughtTypeError:无法读取属性“getExtent”。它应该指向正确的源文件Javascript Openlayers 6地理位置控制,javascript,openlayers,openlayers-6,Javascript,Openlayers,Openlayers 6,我正在重新设计地理位置控制功能,但我不知道我做错了什么。我收到错误消息:UncaughtTypeError:无法读取属性“getExtent”。它应该指向正确的源文件 map.getView().fit(ol.interaction.extent.getExtent(), { maxZoom: 18, duration: 500 }); 完整代码如下: 您错过了本教程的这一部分: const source = new ol.source.Vector(); const laye
map.getView().fit(ol.interaction.extent.getExtent(), {
maxZoom: 18,
duration: 500
});
完整代码如下:
您错过了本教程的这一部分:
const source = new ol.source.Vector();
const layer = new ol.layer.Vector({
source: source
});
map.addLayer(layer);
您更改了以下内容:
map.getView().fit(ol.interaction.extent.getExtent(), {
maxZoom: 18,
duration: 500
});
根据本教程,应该是:
if (!source.isEmpty()) {
map.getView().fit(source.getExtent(), {
maxZoom: 18,
duration: 500
});
}
代码段:
//添加底图
var BaseMap=新建ol.layer.Tile{
标题:谷歌卫星,
基层:是的,
可见:对,
来源:新ol.source.TileImage{
网址:'http://mt1.google.com/vt/lyrs=s&hl=pl&&x={x} &y={y}&z={z}'
}
};
var map=新ol.map{
目标:“地图”,
图层:[底图],
互动时的加载时间:真,
视图:新ol.view{
中心:ol.proj.transform[-76.5,42.45],“EPSG:4326”,“EPSG:3857”,
缩放:11
},
};
const source=新的ol.source.Vector;
常数层=新的ol.layer.Vector{
资料来源:资料来源
};
map.addLayerlayer;
//添加地震动
navigator.geolocation.watchPositionfunctionpos{
常数坐标=[pos.coords.经度,pos.coords.纬度];
常数精度=ol.geom.Polygon.circularcoords,pos.coords.accurity;
source.cleartrue;
source.addFeatures[
新ol.FeatureAccurance.transform'EPSG:4326',map.getView.getProjection,
新ol.功能新ol.geom.Pointol.proj.fromLonLatcoords
];
},函数错误{
警报`错误:${ERROR.message}`;
}, {
EnableHighAccurance:正确
};
const locate=document.createElement'div';
locate.className='ol control ol unselectable locate';
locate.innerHTML=◎';
locate.addEventListener'click'函数{
if!source.isEmpty{
map.getView.fitsource.getExtent{
maxZoom:18,
持续时间:500
};
}
};
map.addControlnew ol.control.control{
元素:定位
};
html,
身体{
宽度:100%;
身高:100%;
填充:0px;
边际:0px;
}
地图{
宽度:100%;
身高:100%;
}
.找到{
文本对齐:右对齐;
底部:0.5em;
右:0.5em;
}