google.maps.Projection 类的作用是什么?

发布于 2024-11-02 22:12:39 字数 1339 浏览 5 评论 0原文

最近,我发现在屏幕上的 LatLngs 和像素坐标之间进行转换的困难方法是我不应该使用 Projection 类可以通过 Map.getProjection 轻松访问,但我应该使用 MapCanvasProjection 只能访问的类通过OverlayView。

如果您正在使用自定义叠加层,则后者非常好用且方便,但如果您没有使用自定义叠加层,则访问 MapCanvasProjection 会非常痛苦。到目前为止,我已经通过向 Map 类添加一个方法为自己解决了这个问题,该方法使我可以轻松访问所需的投影类:

google.maps.Map.prototype.getCanvasProjection = function() {
  if (!this.projectionOverlay) {
    this.projectionOverlay = new google.maps.OverlayView();
    this.projectionOverlay.onAdd = function(){};
    this.projectionOverlay.onRemove = function(){};
    this.projectionOverlay.draw = function(){};
    this.projectionOverlay.setMap(this);
  }
  return this.projectionOverlay.getProjection();
};

这一切看起来像是一个大技巧,可以做一些本应微不足道的事情。这让我更加想知道 google.maps.Projection 类做什么?当我阅读文档时,在我看来, Projection.fromLatLngToPointMapCanvasProjection.fromLatLngToContainerPixel 执行相同的操作,但事实并非如此。我很困惑。

Recently I found out the hard way that to convert between LatLngs and pixel coordinates on screen I shouldn't use the Projection class that is easily accessed through Map.getProjection, but instead I should use the MapCanvasProjection class that can only be accessed through OverlayView.

The latter is nice and handy if you are working with a custom overlay, but if you're not, it's really painful to get access to the MapCanvasProjection. So far I have solved this problem for myself by adding a method to the Map class that will give me easy access to the desired projection class:

google.maps.Map.prototype.getCanvasProjection = function() {
  if (!this.projectionOverlay) {
    this.projectionOverlay = new google.maps.OverlayView();
    this.projectionOverlay.onAdd = function(){};
    this.projectionOverlay.onRemove = function(){};
    this.projectionOverlay.draw = function(){};
    this.projectionOverlay.setMap(this);
  }
  return this.projectionOverlay.getProjection();
};

This all looks like a big hack to do something that should be trivial. And it ever more makes me wonder what does the google.maps.Projection class do? When I read the documentation it seems to me that Projection.fromLatLngToPoint does the same thing as MapCanvasProjection.fromLatLngToContainerPixel, but it does not. I'm puzzled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2024-11-09 22:12:39

Projection.fromLatLngToPoint 表示从纬度经度转换为墨卡托投影。 MapCanvasProjection 进行像素投影(包括墨卡托投影)。

Projection.fromLatLngToPoint means converting from Latitude Longitude to Mercator projection. MapCanvasProjection does a projection to pixel (including Mercator).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文