OpenLayers-进入新搜索时从搜索结果中清除标记

我用OpenLayers,Openstreetmap和BingMaps创建了具有不同图层的地图。我使用了https://github.com/jonataswalker/ol-geocoder中的OpenLayers Control Geocoder(CSS和Javascript文件)。

现在,我想添加该功能,如果输入了新的搜索,则旧标记应自动消失,并显示新的搜索结果。目前,所有标记都将保留在地图上。 我已经在这里对stackoverflow尝试了不同的解决方案,但是到目前为止,它们都没有起作用。谢谢。

这是我main.js中的代码。

import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import Vector from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import Style from 'ol/style/Style';
import Text from 'ol/style/Text';
import Stroke from 'ol/style/Stroke';
import * as olProj from 'ol/proj';
import OSM from 'ol/source/OSM';
import Circle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stamen from 'ol/source/Stamen';
import BingMaps from 'ol/source/BingMaps';
import LayerSwitcher from 'ol/source/ol-layerswitcher';
import Geocoder from 'ol/source/ol-geocoder';

const map = new Map({
  target: 'map',view: new View({
    center: olProj.fromLonLat([16.372,48.209]),zoom: 14
  })
});

//Watercolor
map.addLayer(new TileLayer({
    title: 'watercolor',type: 'base',source: new Stamen({
        layer: 'watercolor',visible: false
    })
}));

//BingMaps
map.addLayer(new TileLayer({
    title: 'Bing',source: new BingMaps({
    key: 'Auj_QkN4cbeT0AIn4Dq-lLk1zsqobx3tpwmekXJgjSFFp0AN48MsogkxCjGZkns9',imagerySet: 'Aerial',visible: false
    })
}));    

//OpenStreetMap
map.addLayer(new TileLayer({
    title: 'OSM',source: new OSM()
}));    

const searchResultsource = new Vector();
const searchResultLayer = new VectorLayer({
  source: searchResultsource
});

searchResultLayer.setStyle(new Style({
  image: new Circle({
    fill: new Fill({
      color: 'rgba(255,255,0.4)'
    }),stroke: new Stroke({
      color: '#3399CC',width: 1.25
    }),radius: 15
  })
}));
map.addLayer(searchResultLayer);
map.addControl(new LayerSwitcher());

//Coordinate Search

var geocoder = new Geocoder('nominatim',{
  provider: 'mapquest',key: 'ACfOgoF7JNAG57XQv72HzpCEoSo8hQmZ',lang: 'de-AT',//en-US,fr-FR
  placeholder: 'Search for...',targetType: 'glass-button',limit: 5,keepOpen: true
});
map.addControl(geocoder);

geocoder.on('addresschosen',function(evt){
  var feature = evt.feature,coord = evt.coordinate,address = evt.address;
  // some popup solution
  content.innerHTML = '<p>'+ address.formatted +'</p>';
  overlay.setPosition(coord);
});

geocoder.on('addresschosen',function(evt) {
  // it's up to you
  console.info(evt);
});

if (layersOnmap) {

function onDrawStart(event)
{
    var features = tempVectorSource.getFeatures();
    for(var i=0;i<features.length;i++)
    {
        features[i].setGeometry(new ol.geom.Point([]));
    }
    tempVectorSource.clear();
}
guolingkai2009 回答:OpenLayers-进入新搜索时从搜索结果中清除标记

最简单的方法是先清除源,然后重新添加当前功能

from selenium.webdriver import ActionChains
import os,time,traceback
from PIL import Image
from io import BytesIO

chrome_options = webdriver.ChromeOptions()


chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless") 
chrome_options.add_argument("--start-maximized") 


PROJECT_ROOT = os.path.abspath(os.path.dirname('__file__'))
DRIVER_BIN = os.path.join(PROJECT_ROOT,"bin/chromedriver")

try:
    browser = webdriver.Chrome(executable_path = DRIVER_BIN,options=chrome_options)
    browser.get('http://somethingurl.com')
    time.sleep(3)

    element = browser.find_element_by_xpath("//div[@id='chartdiv']") # find part of the page you want image of
    time.sleep(1)
    action=ActionChains(browser)
    action.move_to_element_with_offset(element,1020,120) #635,100
    action.perform()
    time.sleep(3)

    location = element.location
    size = element.size
    stress_report_png = browser.get_screenshot_as_png()
    browser.quit()
except:
    traceback.print_exc()
    browser.quit()

stress_report_img = Image.open(BytesIO(stress_report_png)) # uses PIL library to open image in memory
stress_report_img = stress_report_img.crop((1270,330,4000,1320))  
stress_report_img.save('stress_report.png') # saves new cropped image
本文链接:https://www.f2er.com/3111824.html

大家都在问