Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 如何在使用选择交互时获取单击点的坐标?_Vue.js_Openlayers - Fatal编程技术网

Vue.js 如何在使用选择交互时获取单击点的坐标?

Vue.js 如何在使用选择交互时获取单击点的坐标?,vue.js,openlayers,Vue.js,Openlayers,我们在web项目中使用Vue.js和OpenLayers 4.6.5。我们在地图上有很多特征,其中一些是多边形。当我选择某个特定的多边形时,它的样式会变成另一种颜色,这意味着它高亮显示为选中状态。当然,我可以得到选定多边形的坐标。但是,我怎样才能得到我点击的多边形内点的坐标呢 代码如下所示: markObject (mark) { if (!mark) { this.map.un('select', this.onMarkObject) if (this.markSelect

我们在web项目中使用Vue.js和OpenLayers 4.6.5。我们在地图上有很多特征,其中一些是多边形。当我选择某个特定的多边形时,它的样式会变成另一种颜色,这意味着它高亮显示为选中状态。当然,我可以得到选定多边形的坐标。但是,我怎样才能得到我点击的多边形内点的坐标呢

代码如下所示:

markObject (mark) {
  if (!mark) {
    this.map.un('select', this.onMarkObject)
    if (this.markSelection) {
      this.markSelection.getFeatures().remove(this.lastSelectedFeature)
      this.map.removeInteraction(this.markSelection)
    }
    return
  }

  if (!this.markSelection) {
    this.markSelection = new Select({
      condition: condition.click,
      layers: [this.vectorLayer]
    })
    this.markSelection.on('select', this.onMarkObject)
  }

  this.map.addInteraction(this.markSelection)
},
onMarkObject (event) {
  if (event.selected && event.selected.length > 0) {
    const coordinates = event.selected[0].getGeometry().getCoordinates()
  }
}

您需要的是捕获地图上的点击事件,然后将像素转换为地图坐标,看看我为您制作的示例

.地图{ 高度:400px; 宽度:100%; } a{显示:无;} 单击像素坐标 单击地图以获取像素和坐标值

var map=新ol.map{ 目标:“地图”, 图层:[ 新ol.layer.Tile{ 来源:new ol.source.OSM } ], 视图:新ol.view{ 中心:Lonlat的其他项目[37.41,8.82], 缩放:4 } }; 映射。单击,函数EVT{ 常量坐标=map.getCoordinateFrompixelvt.pixel; document.getElementById'p'.innerHTML= `像素:${evt.Pixel[0]}${evt.Pixel[0]}`++ `Coord:${Coord[0].toFixed2}${Coord[1].toFixed2}`; };
事实上,我已经找到了解决方案:

onMarkObject (event) {
    const clickCoordinates = event.mapBrowserEvent.coordinate
    ...
}
无论如何,谢谢你