問題描述
Polygon 的 Google Maps API 可以不提供拖動方法.
The Google Maps API for a Polygon does not offer a drag method.
什么是實現此類功能的有效方法(即,充分優化,不會殺死一臺使用了四年的筆記本電腦)?
What would be an efficient way of implementing such a feature (i.e., sufficiently optimised so that it would not kill a four year old laptop)?
謝謝!
推薦答案
我發現 Google Maps V2 多邊形實現對我的需求非常有限,并通過創建自定義疊加層解決了這個問題.我的小組目前停留在 IE6 上,所以我還沒有遷移到 Google Maps V3 - 但快速瀏覽一下 API 就會發現,您可能可以做與我在 V2 和 V3 中所做的類似的事情.
I found the Google Maps V2 Polygon Implementation to be very limiting for the needs I have had and solved it by creating a custom overlay. My group is currently stuck on IE6 so I have yet to migrate over to Google Maps V3 - but taking a quick look at the API shows that you could probably do a similar thing that I did in V2 with V3.
基本上這個想法是:
- 創建自定義疊加層
- 使用您自己的 SVG/VML 多邊形填充它并將拖動事件附加到此自定義多邊形對象
<小時>
自定義疊加層:
Custom Overlays:
以下信息可幫助您開始制作自己的自定義疊加層:
Here is some information to get you started on making your own custom overlay:
http://code.google.com/apis/地圖/文檔/javascript/overlays.html#CustomOverlays
創建您自己的可拖動"多邊形對象:
Creating your own "Dragable" Polygon Object:
完成后,您需要將自己的多邊形添加到自定義疊加層,而不是使用 GPolygons.我經歷了學習 SVG/VML 并編寫一個庫以將 SVG/VML 連接在一起的痛苦過程——你可以這樣做,但我建議先嘗試使用另一個庫,例如 Rapha?l.
Once you get that down you'll want to add your own polygons to the custom overlay instead of using GPolygons. I went through the painful process of learning SVG/VML and writing a library to bridge SVG/VML together - you could do that, but I would recommend starting by trying to use another library such as Rapha?l.
http://raphaeljs.com/
使用 Rapha?l 將為您節省大量時間來嘗試了解如何獲得跨瀏覽器矢量圖形(多邊形)功能 - 最重要的是它已經支持拖動事件,下面是他們庫中的一個示例:
Using Rapha?l will save you a whole lot of time trying to figure out how to get cross-browser Vector Graphic (Polygon) functionality - and best of all it supports drag events already, here is an example from their library:
http://raphaeljs.com/graffle.html
一旦您有一個自定義疊加層并且您可以將一些 Rapha?l 對象扔到上面,最后一步是將您想要的坐標從 Lat/Lng 值轉換為 Pixel 值.這在 V3 的 MapCanvasProjection 中可用:
Once you have a custom overlay and you are able to throw some Rapha?l objects onto it the last step is to translate the coordinates you want from a Lat/Lng value to a Pixel value. This is available in the MapCanvasProjection of V3:
http://code.google.com/apis/地圖/文檔/javascript/reference.html#MapCanvasProjection
您可以使用 fromLatLngToDivPixel 確定 Raphael 多邊形上的點的實際像素值,繪制它,然后通過拖動事件將其添加到疊加層.
You can use fromLatLngToDivPixel to figure out what the actual pixel values are for the points on your Raphael polygon, draw it, then add it to the overlay with a drag event.
這篇關于使用 Google Maps v3 拖動(移動)多邊形的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!