Adding custom markers dynamically to map using OpenLayers

后端 未结 2 2015
天涯浪人
天涯浪人 2021-02-04 10:03

I would like to let users add custom markers on a map with a description for each marker. Any tips, links to any tutorials would be really useful.

2条回答
  •  我在风中等你
    2021-02-04 10:38

    You can register a function to 'click' event on your map. When the user click on it, the mark is added automatically.

    Try something like this:

    // 'map' is your map created using new OpenLayers.Map(options)
    
    markers = new OpenLayers.Layer.Markers( "Markers" );
    markers.id = "Markers";
    map.addLayer(markers);
    
    map.events.register("click", map, function(e) {
          //var position = this.events.getMousePosition(e);
          var position = map.getLonLatFromPixel(e.xy);
          var size = new OpenLayers.Size(21,25);
       var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
       var icon = new OpenLayers.Icon('images/mark.png', size, offset);   
       var markerslayer = map.getLayer('Markers');
    
       markerslayer.addMarker(new OpenLayers.Marker(position,icon));
    
       });
    

    Pretty simple, but I think that you can understand it :)

提交回复
热议问题