admin管理员组文章数量:1303671
I am using open layers library version 5. I need an onClick event on marker to do some business logic. Anybody could help me with this out thankyou. I have tried every code and snippets. I am using this library to react js.
import Feature from "ol/Feature";
import point from "ol/geom/Point"
import Style from "ol/style/Style";
import Icon from "ol/style/Icon";
renderMap = (lat = 24.8856802, lng = 67.0830459) => {
console.log(lat, lng);
this.map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new OSM()
})
],
view: new View({
center: fromLonLat([lng, lat]),
zoom: 17,
})
});
this.makeMarker(24.8856802, 67.0830459, 0);
}
//here is my marker function
makeMarker = (lat, lng, index) => {
let marker = new Feature({
geometry: new point(fromLonLat([lng, lat])),
});
marker.setStyle(new Style({
image: new Icon(({
// crossOrigin: 'anonymous',
src: require("../../assets/images/location-pin.png"),
enter code here`enter code here`
}))
}));
let vectorSource = new Vector({ features: [marker] })
var markerVectorLayer = new LVector({
source: vectorSource,
});
this.map.addLayer(markerVectorLayer);
marker.on("click", () => {
alert()
})
}
I am using open layers library version 5. I need an onClick event on marker to do some business logic. Anybody could help me with this out thankyou. I have tried every code and snippets. I am using this library to react js.
import Feature from "ol/Feature";
import point from "ol/geom/Point"
import Style from "ol/style/Style";
import Icon from "ol/style/Icon";
renderMap = (lat = 24.8856802, lng = 67.0830459) => {
console.log(lat, lng);
this.map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new OSM()
})
],
view: new View({
center: fromLonLat([lng, lat]),
zoom: 17,
})
});
this.makeMarker(24.8856802, 67.0830459, 0);
}
//here is my marker function
makeMarker = (lat, lng, index) => {
let marker = new Feature({
geometry: new point(fromLonLat([lng, lat])),
});
marker.setStyle(new Style({
image: new Icon(({
// crossOrigin: 'anonymous',
src: require("../../assets/images/location-pin.png"),
enter code here`enter code here`
}))
}));
let vectorSource = new Vector({ features: [marker] })
var markerVectorLayer = new LVector({
source: vectorSource,
});
this.map.addLayer(markerVectorLayer);
marker.on("click", () => {
alert()
})
}
Share
Improve this question
asked May 28, 2019 at 18:29
HafizMuhammad ShoaibHafizMuhammad Shoaib
801 silver badge7 bronze badges
1 Answer
Reset to default 7Features don't have click events. Similar to this example https://openlayers/en/latest/examples/icon.html you will need to listen for click on the map, check if there is a feature at the click pixel and also that feature is your marker
map.on('click', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature) {
return feature;
});
if (feature === marker) {
本文标签: javascriptClick feature on marker of open layers version 5Stack Overflow
版权声明:本文标题:javascript - Click feature on marker of open layers version 5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741720808a2394378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论