简体   繁体   中英

Put a simple point at OpenStreetMap-tile using OpenLayers

I've read in many threads trying to put a simple point (vector layer) at my OpenStreetMap. I'm guessing it is som kind of problem with different projections but i can´t figure it out by myself.

What am i doing wrong in the code below?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://openlayers.org/en/v3.13.1/build/ol.js" type="text/javascript"></script>
    <title>Openstret</title>
</head>
<body>
    <div id="map">
        <script type="text/javascript">

           var vectorSource = new ol.source.Vector();

            var iconFeature = new ol.Feature({
                geometry: new ol.geom.Point([0, 0])
            });

            vectorSource.addFeature(iconFeature);       

            var vectorLayer = new ol.layer.Vector({
                source: vectorSource
            });

            var olmap = new ol.Map({
                view: new ol.View({
                    center: [0, 0],
                    zoom: 2
                }),
                target: 'map'
            });

            var bakgrund = new ol.layer.Tile({source: new ol.source.OSM()});

            olmap.addLayer(bakgrund,vectorLayer);

        </script>

    </div>
</body>
</html>

ol.Map.addLayer only takes one parameter. You'll have to add the two layers separately.

Change

olmap.addLayer(bakgrund,vectorLayer);

to

olmap.addLayer(bakgrund);
olmap.addLayer(vectorLayer);

You're also not including the ol.css file anywhere. Make sure you add that in. Here's a working JSFiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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