简体   繁体   English

具有初始标记的OpenLayer负载图

[英]OpenLayer load map with initial marker

I'm using OpenLayer v4.6.5 to display a map in my html page. 我正在使用OpenLayer v4.6.5在我的html页面中显示地图。 I need to show markers in my map. 我需要在地图上显示标记。 It works fine when clicking on map, but I can't load map initially displaying a marker on it. 单击地图时,它可以正常工作,但我无法加载最初在其上显示标记的地图。 Why the last line of code has no effect? 为什么最后一行代码无效?

<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>

You need to call ol.proj.fromLonLat to convert the coordinates to the correct projection (like you did for the map's center): 您需要调用ol.proj.fromLonLat将坐标转换为正确的投影(就像对地图中心所做的那样):

this.setMarker = function (coordinate) {
  marker = new ol.Feature(
    new ol.geom.Point(ol.proj.fromLonLat(coordinate))
  );
  marker.setStyle(iconStyle);
  vectorSource.addFeature(marker);
}

proof of concept link 概念证明链接

code snippet: 代码段:

 var mapId = "map"; function createMap() { var coordinate = [-117.1610838, 32.715738]; 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: 'https://maps.google.com/mapfiles/ms/micons/blue.png' }) }); var marker; this.setMarker = function(coordinate) { marker = new ol.Feature( new ol.geom.Point(ol.proj.fromLonLat(coordinate)) ); marker.setStyle(iconStyle); vectorSource.addFeature(marker); } return this; } var map = createMap(); map.setMarker([-117.1610838, 32.715738]) 
 html, body { height: 100%; width: 100%; padding: 0px; margin: 0px; } .map { height: 90%; width: 100%; } 
 <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script> <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css"> <div id="map" class="map"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM