我知道該方法存在並且有記錄,但我不知道如何獲取MapCanvasProjection對象。如何在Google Maps API V3中調用fromLatLngToDivPixel?
回答
var overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map);
var point = overlay.getProjection().fromLatLngToDivPixel(latLng);
醜確實如此。在v2中更容易 - 谷歌api v3的另一個缺陷!
爲了讓您能夠從OverlayView派生類MapCanvasProjection和調用,它返回一個MapCanvasProjection型
使用onAdd()的getProjection()方法,畫()和onRemove()必須實現從OverlayView的派生。
function MyOverlay(options) {
this.setValues(options);
var div = this.div_= document.createElement('div');
div.className = "overlay";
};
// MyOverlay is derived from google.maps.OverlayView
MyOverlay.prototype = new google.maps.OverlayView;
MyOverlay.prototype.onAdd = function() {
var pane = this.getPanes().overlayLayer;
pane.appendChild(this.div_);
}
MyOverlay.prototype.onRemove = function() {
this.div_.parentNode.removeChild(this.div_);
}
MyOverlay.prototype.draw = function() {
var projection = this.getProjection();
var position = projection.fromLatLngToDivPixel(this.getMap().getCenter());
var div = this.div_;
div.style.left = position.x + 'px';
div.style.top = position.y + 'px';
div.style.display = 'block';
};
那麼當你創建地圖
var OverLayMap = new MyOverlay({ map: map });
對於V2 你應該能夠從您的GMap2實例調用fromLatLngToDivPixel
var centerPoint = map.fromLatLngToDivPixel(map.getCenter());
是不是將API v2與v3混合? – 2009-10-12 19:26:32
這些是從V3 – 2009-10-12 19:28:02
現在您的上次編輯,沒關係 – 2009-10-12 19:34:52
var map;
// Create your map
MyOverlay.prototype = new google.maps.OverlayView();
MyOverlay.prototype.onAdd = function() { }
MyOverlay.prototype.onRemove = function() { }
MyOverlay.prototype.draw = function() { }
function MyOverlay(map) { this.setMap(map); }
var overlay = new MyOverlay(map);
var projection = overlay.getProjection();
我試過這段代碼,並且變量投影看起來是「未定義的」。任何想法爲什麼?謝謝! – LaundroMat 2010-11-13 21:28:19
注意:(這讓我難倒了半小時) - 確保在創建地圖時創建疊加層,而不是在需要座標時(通過fromLatLngToDivPixel)。如果你這樣做,你會得到Uncaught TypeError:不能調用未定義的fromLatLngToDivPixel方法。它看起來像谷歌異步做這些東西。 – AlexeyMK 2011-08-11 22:37:36
的代碼可以大大簡化,看我的回答 – TMS 2011-11-23 10:00:43
我沒對這裏的答案不滿意。所以我做了一些實驗,找到了「最簡單」的工作解決方案,這與拉爾夫的答案很接近,但希望更容易理解。 (我希望谷歌讓這個功能方便使用!)
首先聲明的OverlayView
一個子類的地方,像這樣:
function CanvasProjectionOverlay() {}
CanvasProjectionOverlay.prototype = new google.maps.OverlayView();
CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay;
CanvasProjectionOverlay.prototype.onAdd = function(){};
CanvasProjectionOverlay.prototype.draw = function(){};
CanvasProjectionOverlay.prototype.onRemove = function(){};
然後其他地方在你的代碼中實例化地圖,你也實例這OverlayView的並設置其地圖,就像這樣:
var map = new google.maps.Map(document.getElementById('google-map'), mapOptions);
// Add canvas projection overlay so we can use the LatLng to pixel converter
var canvasProjectionOverlay = new CanvasProjectionOverlay();
canvasProjectionOverlay.setMap(map);
然後,每當你需要使用fromLatLngToContainerPixel
,你只是這樣做:
canvasProjectionOverlay.getProjection().fromLatLngToContainerPixel(myLatLng);
注意,因爲MapCanvasProjection對象將只提供一次draw()
被調用,這是地圖的idle
,我建議創建一個布爾「mapInitialized」標誌之前的某個時候,將其設置爲true的第一個地圖idle
回調。然後在那之後做你需要做的事情。
我認爲最簡單的方法是忽略谷歌希望通過刪除和隱藏有用的功能而不是添加新的功能來使我們的生活更加艱難,而只是編寫自己的方法來做同樣的事情。
這裏是一個功能某人的版本發佈在其他地方(現在我不能找到它),爲我工作:
fromLatLngToPixel: function (position) {
var scale = Math.pow(2, Map.getZoom());
var proj = Map.getProjection();
var bounds = Map.getBounds();
var nw = proj.fromLatLngToPoint(
new google.maps.LatLng(
bounds.getNorthEast().lat(),
bounds.getSouthWest().lng()
));
var point = proj.fromLatLngToPoint(position);
return new google.maps.Point(
Math.floor((point.x - nw.x) * scale),
Math.floor((point.y - nw.y) * scale));
},
現在,你可以把它任何時候,任何地方你想要的。我特別需要它來定製上下文菜單,而且它完美地完成了工作。
編輯:我也寫了相反的作用,fromPixelToLatLng,做完全相反。它只是基於第一個,適用於一些數學運算:
fromPixelToLatLng: function (pixel) {
var scale = Math.pow(2, Map.getZoom());
var proj = Map.getProjection();
var bounds = Map.getBounds();
var nw = proj.fromLatLngToPoint(
new google.maps.LatLng(
bounds.getNorthEast().lat(),
bounds.getSouthWest().lng()
));
var point = new google.maps.Point();
point.x = pixel.x/scale + nw.x;
point.y = pixel.y/scale + nw.y;
return proj.fromPointToLatLng(point);
}
- 1. Google Maps API v2 vs Google Maps API v3?
- 2. Google Maps API v3 BrowserIsCompatible
- 3. Google Maps API v3 - maxZoomService
- 4. Google Maps API V3:fromContainerPixelToLatLng
- 5. 在Google Maps API V3中使用Choropleth
- 6. Google Maps API v3 - 如何指定「VehicleType」
- 7. Google maps API V3方法fitBounds()
- 8. google maps api v3 + infoBubble in markerClusterer
- 9. kml + google maps api v3 + geoxml3
- 10. Google Maps API V3 - Javascript問題
- 11. Google Maps API V3 - Internet Explorer
- 12. Google maps api v3 + marker clusterer:centering map?
- 13. Google Maps Api v3 - getBounds is undefined
- 14. Google Maps API v3覆蓋
- 15. Google Maps API V3返回610,0,0,0
- 16. Google Maps API v3 directions.route()請求
- 17. 在Unity 3D上使用Google Maps API v3
- 18. 在Google Maps API V3中繪製標記
- 19. Google Maps API V3 - 子類google.maps.Marker:如何調用父級方法?
- 20. set_at在Google Maps V3 API中多次調用
- 21. 如何在arc2earth中使用google maps api v3?
- 22. 如何在Javascript中使用Google Maps InfoWindow(v3 API)
- 23. 從Windows Forms應用程序調用Google Maps API V3 JavaScript API?
- 24. Google Maps API v3:爲什麼Google Maps API V3和Google Maps的搜索結果之間存在不一致?
- 25. Google Maps API v3街景「google未定義」
- 26. 如何在Google Maps API v3中設置點陣列的中心
- 27. disableAutoPan無法使用Google Maps API v3
- 28. Mapkit是否使用Google Maps API V3?
- 29. Safari 6.0.2中的Google Maps API v3 *破損*
- 30. Google Maps API v3中的路線距離
這對我來說完全適用,但如果我平移地圖,像素位置不會更新。但是,如果我改變zomm級別,它會再次按預期工作(直到下一個平移)。我錯過了什麼嗎? – 2012-03-06 17:47:28
關於以前的評論,使用fromLatLngToContainerPixel而不是fromLatLngToDivPixel解決了我的問題,即在平移地圖後像素位置不更新。 – 2012-03-13 07:59:56
['google.maps.MapCanvasProjection'對象規範,其中包括'fromLatLngToContainerPixel(latLng:LatLng)'](https://developers.google.com/maps/documentation/javascript/reference?csw=1#MapCanvasProjection) – user664833 2014-06-22 06:41:44