google.maps.Projection 类的作用是什么?
最近,我发现在屏幕上的 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.fromLatLngToPoint
与 MapCanvasProjection.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Projection.fromLatLngToPoint 表示从纬度经度转换为墨卡托投影。 MapCanvasProjection 进行像素投影(包括墨卡托投影)。
Projection.fromLatLngToPoint means converting from Latitude Longitude to Mercator projection. MapCanvasProjection does a projection to pixel (including Mercator).