マップ上にマーカーを置くことはできませんが、その位置は動的に更新されます。 ジオロケーションの関数内でレイヤーを作成すると( 'change'イベントは機能しますが、ジオロケーションが変更されるたびにレイヤーが追加されるため、その関数の外にレイヤーを作成し、マーカーfolowingコードでOpenlayersでのジオロケーションマーカーの位置の更新3
私は 'TypeError例外を:aがnullである' 取得。
var geolocation = new ol.Geolocation({
projection: map.getView().getProjection(),
tracking: true,
trackingOptions: {
enableHighAccuracy: true,
maximumAge: 2000
}
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: './_img/marker_.png'
})
});
var pos1 = geolocation.getPosition();
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(pos1)
});
var iconSource = new ol.source.Vector({
features: [iconFeature]
});
var iconLayer = new ol.layer.Vector({
source: iconSource,
style : iconStyle
});
map.addLayer(iconLayer);
geolocation.on('change', function() {
var pos_upd = geolocation.getPosition();
iconFeature.getGeometry().setCoordinates(pos_upd);
view.setCenter(pos_upd);
view.setZoom(18);
});