如何将这个google maps v2 API函数转换为v3?

时间:2013-01-29 20:53:20

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

我需要将此功能转换为v3 API。选中此功能用于在标记下创建椭圆。我尝试过投影和叠加,但没有成功。

  function makePolyPoints(_marker)
  {
    var polyPoints = Array();
    var markerPoint=  _marker.getLatLng();
    var projection = G_NORMAL_MAP.getProjection();
    var mapZoom = map.getZoom();
    var clickedPixel = projection.fromLatLngToPixel(markerPoint, mapZoom);
    var  ellipseRadA = 20;
    var ellipseRadB = 10;

    var polyNumSides = 20;
    var polySideLength = 18;

    for (var a = 0; a<(polyNumSides+1); a++) {
        var aRad = polySideLength*a*(Math.PI/180);
        var pixelX =  clickedPixel.x + ellipseRadA * Math.cos(aRad);
        var pixelY = -3 + clickedPixel.y + ellipseRadB * Math.sin(aRad);
        var polyPixel = new GPoint(pixelX,pixelY);
        var polyPoint = projection.fromPixelToLatLng(polyPixel,mapZoom);
        polyPoints.push(polyPoint);
    }
    return polyPoints;
}

这里v3的功能不起作用,我认为缩放级别的问题,但我找不到如何替换projection.fromLatLngToPixel(markerPoint,mapZoom);

function makePolyPoints(_marker)
{
    var polyPoints = Array();
    var markerPoint=  _marker.getPosition();
    var projection = map.getProjection();
    var mapZoom = map.getZoom();
    var clickedPixel = projection.fromLatLngToPoint(markerPoint);

    var  ellipseRadA = 20;
    var ellipseRadB = 10;

    var polyNumSides = 20;
    var polySideLength = 18;

    for (var a = 0; a<(polyNumSides+1); a++) {
        var aRad = polySideLength*a*(Math.PI/180);
        var pixelX =  clickedPixel.x + ellipseRadA * Math.cos(aRad);
        var pixelY = -3 + clickedPixel.y + ellipseRadB * Math.sin(aRad);
        var polyPixel = new google.maps.Point(pixelX,pixelY);
        var polyPoint = projection.fromPointToLatLng(polyPixel);
        polyPoints.push(polyPoint);
    }
    return polyPoints;
}

1 个答案:

答案 0 :(得分:3)

嗯..我将使用我最喜欢的一个gmap示例来帮助你。

使用var clickedPixel = projection.fromLatLngToPoint(markerPoint);不会给你点击的像素坐标,它们仍然是世界坐标。请参阅projection specs

在计算那种东西时指定自己的墨卡托投影很有用。仔细查看以下几行:(in this code example

var worldCoordinate = projection.fromLatLngToPoint(chicago);
    var pixelCoordinate = new google.maps.Point(
    worldCoordinate.x * numTiles,
    worldCoordinate.y * numTiles);

(你可以用firebug或类似的东西查看来源)。

如果你没有它(在mozilla中:工具 - &gt; web开发者 - &gt;页面来源)

希望这可以帮助您解决问题:)