Я использую OpenLayer v4.6.5 для отображения карты на моей html-странице.Мне нужно показать маркеры на моей карте.Он отлично работает при нажатии на карту, но я не могу загрузить карту, на которой изначально отображается маркер.Почему последняя строка кода не имеет никакого эффекта?
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script>
function createMap(){
var coordinate = someCoordinate;
var vectorSource = new ol.source.Vector({});
var vectorLayer = new ol.layer.Vector({ source: vectorSource });
var view = new ol.View({ center: ol.proj.fromLonLat(coordinate), zoom: 12, maxZoom: 19, minZoom: 5 });
var map = new ol.Map({
layers: [new ol.layer.Tile({ source: new ol.source.OSM({ key: 'myKey', crossOrigin: '' }) }), vectorLayer,],
target: document.getElementById(mapId),
controls: ol.control.defaults(),
view: view
});
// create custom marker image with custom text in bottom
var iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [12, 37],
anchorXUnits: 'pixels', //'fraction'
anchorYUnits: 'pixels',
opacity: 0.8,
src: 'marker.png'
})
});
var marker;
this.setMarker = function (coordinate) {
marker = new ol.Feature(
new ol.geom.Point(coordinate)
);
marker.setStyle(iconStyle);
vectorSource.addFeature(marker);
}
map.on('click', function (evt) {
setMarker(evt.coordinate);
});
return this;
}
var map = createMap();
// NOTE: This line of code has no effect!!!
map.setMarker(someCoordinate)
</script>