使用fromLatLngToPoint()将V2转换为V3时,值不会到来

时间:2013-06-20 13:48:19

标签: google-maps google-maps-api-3 google-maps-api-2

我正在将代码V2转换为V3,以下代码为google map V2代码。在警报sw.x中有一些价值即将来临。

//Google map V2 code:
function flagIntersectingMarkers()  {
   var pad = this.borderPadding;
       var zoom = this.map.getZoom(); 
   var projection = this.map.getCurrentMapType().getProjection();
   var bounds = this.map.getBounds();
   var sw = bounds.getSouthWest();
   sw = projection.fromLatLngToPixel(sw, zoom);
   alert("sw"+sw.x); // In this alert some value is coming
   sw = new GPoint(sw.x-pad, sw.y+pad);
  sw = projection.fromPixelToLatLng(sw, zoom, true);
 }

//Google map V3 code:
function flagIntersectingMarkers()  {
   var pad = this.borderPadding;
       var zoom = this.map.getZoom();
   var projection = this.map.getProjection();
   var bounds   = this.map.getBounds();
   var sw = bounds.getSouthWest();
   sw = projection.fromLatLngToPoint(sw, zoom);
   alert("sw"+sw.x); // Undefined value is coming
   sw = new google.maps.Point(sw.x-pad, sw.y+pad);
   sw = projection.fromPointToLatLng(sw, zoom, true);
 }

但在上面的V3代码中,在警告sw.x未定义的值即将到来时,如何检索sw.x中的V3值。

1 个答案:

答案 0 :(得分:2)

您遇到的问题是您没有正确地将某些调用从v2转换为v3,并且没有检查方法的参数列表。确保you're using the latest API docs

//Google map V3 code:
function flagIntersectingMarkers()  {
   var pad = this.borderPadding;
   var zoom = this.map.getZoom();              // Returns number
   var projection = this.map.getProjection();  // Returns Projection
   var bounds   = this.map.getBounds();        // Returns LatLngBounds
   var sw = bounds.getSouthWest();             // Returns LatLng
   var swpt = projection.fromLatLngToPoint(sw); // WRONG in original code 2nd argument is Point, and not needed, plus should not overwrite sw
   alert("swpt"+swpt.x); // Should be defined now
   swptnew = new google.maps.Point(swpt.x-pad, swpt.y+pad); // Build new Point with modded x and y
   swnew = projection.fromPointToLatLng(swptnew, true);  //Build new LatLng with new Point, no second argument, true for nowrap
}