javascript - getPixelFromCoordinate 返回 null

标签 javascript mapquest

在我的应用程序中,我添加了一个包含开放层 3、绘制点和线的 Mapquest 层...

现在我需要使用

了解 map 中长/纬度点(可见区域)的像素坐标
map.getPixelFromCoordinate(coordinate).

此函数始终返回null(测试):

center = map.getView().getCenter();
px = map.getPixelFromCoordinate(center);
alert(JSON.stringify(px));

我做错了什么或者我没有正确理解什么?

最佳答案

我找到了这个答案:

I'd be careful with this. You might get wrong results, e.g. when the map does not have the final layout yet. It is better to wait with the first coordinate to pixel conversion until the map is rendered. You do not need a timeout for this, we have the 'postrender' event on ol.Map. So in your initialisation code, you could do something like this:

map.once('postrender', function() {
  // safe to call map.getPixelFromCoordinate from now on
});

来源:github.com/openlayers/ol3/issues/5456

我希望这会有所帮助。

关于javascript - getPixelFromCoordinate 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37720736/

相关文章:

javascript - 将静态 map 保存为 Base64 图像? - map 探索

Android map - 使用布局作为叠加项

javascript - MapQuest Leaflet Api - 获取时间和距离的优化路径

javascript - 属性 :desc is not the same as reverse 上的 Ember sortBy

javascript - 无需 gulp 即可浏览 ES6 代码

javascript - 如何将数组从 observable 映射到另一个数组

javascript - 检查 div 是否显示其所有内容的最佳实践

java - 是否存在我不知道的 OpenStreetMap/MapQuest 托管的 map 图 block 属性?

ios - MQMapView 设置区域 : causes app to crash

javascript - 内联 Angular 加载模板