简体   繁体   English

使用OpenLayers 3添加标记

[英]Adding marker using OpenLayers 3

I want to add a simple marker on on any longitude and latitude (location). 我想在任何经度和纬度(位置)上添加一个简单的标记。 I have Googled it a lot but didn't found any sensible answer or help from there. 我已经在Google上搜索了很多,但是没有从那里找到任何明智的答案或帮助。 Some code I have tried but they were not working fine for me. 我尝试了一些代码,但对我而言它们不能正常工作。 I am not good in programming and reading the existing code. 我不太擅长编程和阅读现有代码。

Please go here http://openlayers.org/en/latest/examples/icon.html and see this is good example and showing the icon but behind that there is an image not any map. 请转到此处http://openlayers.org/en/latest/examples/icon.html ,看到这是一个很好的示例,并显示了图标,但在其后面没有任何地图。 I have tried to show any map there and show marker with longitude and latitude but its not working from my end. 我试图显示那里的任何地图,并显示经度和纬度的标记,但是从我的角度来看它不起作用。

I just want to show a simple map with marker on any particular location. 我只想在任何特定位置显示带有标记的简单地图。 On click the popup should open and should contain information about that location 单击后,弹出窗口应打开,并应包含有关该位置的信息

Eg This is a xyz restaurant, xyz hotel, xyz temple etc. 例如,这是一家xyz餐厅,xyz酒店,xyz神庙等。

if you want to display a custom content in your popup when clicking on your marker : 如果要在单击标记时在弹出窗口中显示自定义内容,请执行以下操作:

 $(element).popover({
        'placement': 'top',
        'html': true,
        'content': "this is a "+feature.get('type')+" in location : "+feature.getGeometry().getCoordinates().toString()
      });

if the example in the link you shared isn't working for you can you show the errors it is returning in the console 如果您共享的链接中的示例不起作用,可以在控制台中显示它返回的错误

The background map in the example you linked was designed to look like an image. 您链接的示例中的背景图被设计为看起来像图像。 It is pulled in with the following code: 使用以下代码将其插入:

  var rasterLayer = new ol.layer.Tile({
    source: new ol.source.TileJSON({
      url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.json',
      crossOrigin: ''
    })
  });

If you want a different map, you can replace the tile layer source with any of the sources available in OpenLayers, or with your custom one. 如果要使用其他地图,则可以用OpenLayers中可用的任何来源或自定义的来源替换切片图层来源。 Maybe OpenStreetMap would be an option? 也许可以选择OpenStreetMap? Just replace the above snippet with the following: 只需将以下代码段替换为以下代码:

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

The code that positions the popup and sets its content in that example is 在该示例中放置弹出窗口并设置其内容的代码是

      var coordinates = feature.getGeometry().getCoordinates();
      popup.setPosition(coordinates);
      $(element).popover({
        'placement': 'top',
        'html': true,
        'content': feature.get('name')
      });

To also display latitude and longitude, change that code to 要同时显示纬度和经度,请将代码更改为

      var coordinates = feature.getGeometry().getCoordinates();
      popup.setPosition(coordinates);
      $(element).popover({
        'placement': 'top',
        'html': true,
        'content': feature.get('name') + ' ' + ol.coordinate.toStringHDMS(coordinates, 1)
      });

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

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