Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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
如何从ReactJS中的OpenLayers地图中获取坐标?_Reactjs_Openlayers_Openlayers 3 - Fatal编程技术网

如何从ReactJS中的OpenLayers地图中获取坐标?

如何从ReactJS中的OpenLayers地图中获取坐标?,reactjs,openlayers,openlayers-3,Reactjs,Openlayers,Openlayers 3,在开放图层地图中,有三个位置以三角形连接。我试图在OpenLayers和React JS的帮助下获得所有三个位置(纬度和经度)。但不幸的是,我能够获得可见视图的纬度和经度,而不是标记的图层 当我使用下面的代码时,它不会获取预期的long和lat,而是生成可见的map long和lat var glbox = map.getView().calculateExtent(map.getSize()); var box = proj.transformExtent(glbox,'EPSG:3857'

在开放图层地图中,有三个位置以三角形连接。我试图在OpenLayers和React JS的帮助下获得所有三个位置(纬度和经度)。但不幸的是,我能够获得可见视图的纬度和经度,而不是标记的图层

当我使用下面的代码时,它不会获取预期的long和lat,而是生成可见的map long和lat

var glbox = map.getView().calculateExtent(map.getSize()); 
var box = proj.transformExtent(glbox,'EPSG:3857','EPSG:4326'); 
console.log("Latitude and longitude :",box);
console.log("Long and Lat :",map.getFeaturesAtPixel());  //--> null
console.log("Long and Lat :",map.getLayers());  
console.log("Long and Lat :",map.getFeaturesAtPixel());  //--> null
因此,我也尝试了以下选项,但并没有得到预期的long和lat

var glbox = map.getView().calculateExtent(map.getSize()); 
var box = proj.transformExtent(glbox,'EPSG:3857','EPSG:4326'); 
console.log("Latitude and longitude :",box);
console.log("Long and Lat :",map.getFeaturesAtPixel());  //--> null
console.log("Long and Lat :",map.getLayers());  
console.log("Long and Lat :",map.getFeaturesAtPixel());  //--> null
如何获得图像中显示的所有三个位置的纬度和经度


它永远不会像你现在这样工作。 我是什么意思?我的意思是,浏览map.getFeaturesAtPixel是一种可行的方法,但您没有阅读。您至少需要为函数提供像素(x,y屏幕坐标)

您可以使用以下方法获得像素

map.on('click', evt => {
  console.log(evt.pixel);
})
我做了一个简单的演示来说明。转到并在浏览器调试器控制台中粘贴以下代码。单击点并观察控制台中的行为

map.on('click', evt => {
  var features = map.getFeaturesAtPixel(evt.pixel);
  if (features) {
    // Get name (but it depends of your data attributes)
    console.log(features
      .filter(feature => feature.getGeometry().getType() == 'Point')
      .map(feature => feature.get('name')));
    // Get the features, filter to only get Point, then get geometry
    // and coordinates, change projection to lon lat
    console.log(features
      .filter(feature => feature.getGeometry().getType() == 'Point')
      .map(feature => `Longitude, latitude: ${ol.proj.toLonLat(feature.getGeometry().getCoordinates()).join(', ')}`));
  }
})
根据反馈进行编辑。

要从线字符串获取点,只需执行以下操作

var featuresLinestringPointsCoordinates = vector.getSource().getFeatures()
  .map(feature => {
    return feature
      .getGeometry()
      .clone()
      .transform('EPSG:3857','EPSG:4326')
     .getCoordinates();
});
console.log(featuresLinestringPointsCoordinates);
// More readable and we only get the first linestring
console.table(featuresLinestringPointsCoordinates[0])

在绘制线串后进行测试

谢谢!!!当我在地图上做一些点击动作时,它绝对有效。但我想提取地图中已经绘制的形状的经纬度。您绘制的形状是一条带有三段的线串?无法从图形中猜测。此外,当您说需要坐标时,不确定是线串的坐标还是线串的点?线串的点