如何在Google Maps API V3中调用fromLatLngToDivPixel?
我知道该方法存在并已记录,但我不知道如何获取 MapCanvasProjection 对象。
I know that method exists and is documented, but I don't know how to get an MapCanvasProjection object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看看 http://qfox.nl/notes/116
确实丑陋。 v2 更容易 - google api v3 的另一个缺陷!
Look at http://qfox.nl/notes/116
Ugly indeed. Much easier in v2 - another flaw of google api v3!
我认为最简单的方法是忽略谷歌通过删除和隐藏有用的功能而不是添加新功能来让我们的生活变得更加困难的愿望,而只是编写自己的方法来做同样的事情。
这是某人在其他地方发布的函数版本(我现在找不到它),对我有用:
现在您可以随时随地调用它。我特别需要它来自定义上下文菜单,它完美地完成了它的工作。
编辑:我还编写了一个反向函数,fromPixelToLatLng,它的作用完全相反。它只是基于第一个,并应用了一些数学:
I think the easiest way is to ignore Google's desire to make our life harder by removing and hiding useful functions instead of adding new ones, and just to write your own methods that do the same thing.
Here's a version of a function somebody posted somewhere else (I can't find it right now), that worked for me:
Now you can call it any time and any where you want. I especially needed it for custom context menus, and it does it's job perfectly.
EDIT: I also wrote a reverse function, fromPixelToLatLng that does exactly the opposite. It is simply based on the first one, with some math applied:
我对这里的答案不满意。所以我做了一些实验,找到了“最简单”的工作解决方案,它接近拉尔夫的答案,但希望更容易理解。 (我希望 Google 使此功能更易于访问!)
首先,您在某个地方声明
OverlayView
的子类,如下所示:然后在实例化地图的代码中的其他地方,您还可以实例化此 OverlayView 并设置其地图,如下所示:
然后,每当您需要使用
fromLatLngToContainerPixel
时,只需执行以下操作:请注意,因为 MapCanvasProjection 对象仅可用一次
draw()< /code> 被调用,这是在地图
idle
之前的某个时间,我建议创建一个布尔值“mapInitialized”标志,在第一个地图idle
回调中将其设置为 true。然后再做你需要做的事情。I wasn't satisfied with the answers here. So I did some experiments and found the "simplest" working solution, which is close to Ralph's answer, but hopefully more understandable. (I wish Google makes this feature more accessible!)
First you declare a subclass of
OverlayView
somewhere like so:Then somewhere else in your code where you instantiate the map, you also instantiate this OverlayView and set its map, like so:
Then, whenever you need to use
fromLatLngToContainerPixel
, you just do this:Note that because the MapCanvasProjection object will only be available once
draw()
is called, which is sometime before the map'sidle
, I suggest creating a boolean "mapInitialized" flag, set it to true on the first mapidle
callback. And then do what you need to do only after that.要获取 MapCanvasProjection,您可以从 OverlayView 派生一个类并调用 getProjection() 方法,该方法返回 MapCanvasProjection 类型
必须实现 onAdd()、draw() 和 onRemove()
创建地图时
才能从 OverlayView 派生。然后当您对于 V2
您应该能够从 GMap2 实例调用 fromLatLngToDivPixel
To get a MapCanvasProjection you could derive a class from OverlayView and call the getProjection() method which returns a MapCanvasProjection type
onAdd(), draw() and onRemove() must be implemented to derive from OverlayView.
then when you're creating your map
For V2
you should be able to call fromLatLngToDivPixel from your GMap2 instance