简体   繁体   English

如何使用OSM在openstreetmap中获取标记或任何图标

[英]how to get marker or any icon in openstreetmap using OSM

Im using Ol V5.3.1 and working with asp mvc and this is my code for getting the map and a marker or any icon in a specific location to show my location 我正在使用Ol V5.3.1并与asp mvc一起工作,这是我的代码,用于获取特定位置的地图和标记或任何图标以显示我的位置

 var view = new ol.View({
            center: ol.proj.fromLonLat([51.40876293182373, 35.757448286487595]),
            zoom: 12,
            maxZoom: 19,
        });

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

        var VactorLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: new ol.Feature({
                    geometry: new ol.geom.Point([51, 35]),
                    name: 'Null Island',
                    population: 4000,
                    rainfall: 500,
                    setStyle: new ol.style.Style({
                        image: new ol.style.Icon({
                            anchor: [0.5, 46],
                            anchorXUnits: 'fraction',
                            anchorYUnits: 'pixels',
                            src: "http://openlayers.org/en/v3.7.0/examples/data/icon.png",
                        })
                    })
                })
            })
        });

var map = new ol.Map({
            target: 'map',
            controls: new ol.control.defaults({
                attributionOptions: {
                    collapsible: false
                }
            }),
            layers: [rasterLayer, VactorLayer],
            view: view,
        });

everything is good and showing the tiles and map with my location and there's no any error in console of my browser for javascripts but just dont show the icon and marker as this is an example of openlayer example working with vector layer at Example_Marker 一切都很好,并显示了带有我所在位置的图块和地图,并且在我的JavaScript浏览器的控制台中没有任何错误,但仅不显示图标和标记,因为这是在Example_Marker中使用矢量层的openlayer示例的示例

i also find another way to put marker with overlay in this Example as i code this in below 我也在本示例中找到了另一种放置带有叠加标记的方法,因为我在下面编写了代码

 var marker = new ol.Overlay({
            position: pos,
            positioning: 'center-center',
            element: document.getElementById('marker'),
            stopEvent: false
        });

    var pos = new ol.proj.fromLonLat([51, 35]);

        map.addOverlay(marker);

but also not showing this one 但也没有显示这一

any suggestion? 有什么建议吗?

Your code is different from the example with the marker 您的代码与带有标记示例不同

  1. The position of marker isn't transformed into the projection of the map (like you do with the center ): 标记的位置不会转换为地图的投影(就像您使用center ):
var VactorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: new ol.Feature({
                geometry: new ol.geom.Point([51, 35]),

should be: 应该:

var VactorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: new ol.Feature({
                geometry: new ol.geom.Point(ol.proj.fromLonLat([51, 35])),
  1. The features array in the VectorSource should be an array: VectorSourcefeatures数组应该是一个数组:
var VactorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: new ol.Feature({
                geometry: new ol.geom.Point([51, 35]),
                name: 'Null Island',
                population: 4000,
                rainfall: 500,
            })

should be: 应该:

var VactorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: [new ol.Feature({
                geometry: new ol.geom.Point([51, 35]),
                name: 'Null Island',
                population: 4000,
                rainfall: 500,
             })] 

Then the "marker" appears, but doesn't have the correct style. 然后出现“标记”,但样式不正确。

code snippet: 代码段:

 html, body { height: 100%; width: 100%; padding: 0px; margin: 0px; } .map { height: 95%; width: 100%; } 
 <link rel="stylesheet" href="https://openlayers.org/en/v5.1.3/css/ol.css" type="text/css"> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/build/ol.js"></script> <div id="map" class="map"></div> <script> var view = new ol.View({ center: ol.proj.fromLonLat([51.40876293182373, 35.757448286487595]), zoom: 6, maxZoom: 19, }); var rasterLayer = new ol.layer.Tile({ source: new ol.source.OSM() }); var VactorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([51, 35])), name: 'Null Island', population: 4000, rainfall: 500, setStyle: new ol.style.Style({ image: new ol.style.Icon({ anchor: [0.5, 46], anchorXUnits: 'fraction', anchorYUnits: 'pixels', src: "http://openlayers.org/en/v3.7.0/examples/data/icon.png", }) }) })] }) }); var map = new ol.Map({ target: 'map', controls: new ol.control.defaults({ attributionOptions: { collapsible: false } }), layers: [rasterLayer, VactorLayer], view: view, }); </script> 

生成的地图的屏幕截图

To use the same Icon as in the example, create it as a "feature" and call setStyle on the feature (there is no setStyle property of a feature). 要使用与示例中相同的Icon,请将其创建为“功能”并在功能上调用setStyle(功能没有setStyle属性)。

var iconFeature = new ol.Feature({
            geometry: new ol.geom.Point(ol.proj.fromLonLat([51, 35])),
            name: 'Null Island',
            population: 4000,
            rainfall: 500,
        });
iconFeature.setStyle(new ol.style.Style({
                image: new ol.style.Icon({
                    anchor: [0.5, 46],
                    anchorXUnits: 'fraction',
                    anchorYUnits: 'pixels',
                    src: "http://openlayers.org/en/v3.7.0/examples/data/icon.png",
                })
            }));
var VactorLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
        features: [iconFeature]
    })
});

code snippet: 代码段:

 html, body { height: 100%; width: 100%; padding: 0px; margin: 0px; } .map { height: 95%; width: 100%; } 
 <link rel="stylesheet" href="https://openlayers.org/en/v5.1.3/css/ol.css" type="text/css"> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/build/ol.js"></script> <div id="map" class="map"></div> <script> var view = new ol.View({ center: ol.proj.fromLonLat([51.40876293182373, 35.757448286487595]), zoom: 7, maxZoom: 19, }); var rasterLayer = new ol.layer.Tile({ source: new ol.source.OSM() }); var iconFeature = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([51, 35])), name: 'Null Island', population: 4000, rainfall: 500, }); iconFeature.setStyle(new ol.style.Style({ image: new ol.style.Icon({ anchor: [0.5, 46], anchorXUnits: 'fraction', anchorYUnits: 'pixels', src: "http://openlayers.org/en/v3.7.0/examples/data/icon.png", }) })); var VactorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [iconFeature] }) }); var map = new ol.Map({ target: 'map', controls: new ol.control.defaults({ attributionOptions: { collapsible: false } }), layers: [rasterLayer, VactorLayer], view: view, }); </script> 

生成的地图的屏幕截图

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

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