【问题标题】:how to get the location(lat/lng) on google maps v3 from the location(x,y)如何从位置(x,y)获取 google maps v3 上的位置(lat/lng)
【发布时间】:2011-04-03 19:36:07
【问题描述】:

我有 (x,y)

(event.pageX-$("#map_canvas").offset().left,event.pageY-$("#map_canvas").offset().top)

但是如何获得 (lat,lng)

谢谢

【问题讨论】:

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


    【解决方案1】:

    在地图定义的初始化函数中:

    var overlay;
    overlay = new google.maps.OverlayView();
    overlay.draw = function() {};
    overlay.setMap(map);
    

    下一步当你想要运行位置时:

    var point=new google.maps.Point(x,y);
    var location=$overlay.getProjection().fromContainerPixelToLatLng(point); 
    

    最好的 克

    【讨论】:

      【解决方案2】:

      输入左侧和顶部像素偏移 (x,y) 并返回 google.maps.LatLng

      function PixelToLatLng(x,y){
          var overlay = new google.maps.OverlayView()
          overlay.setMap(map);
          var LatLng = overlay.getProjection().fromContainerPixelToLatLng(new google.maps.Point(x,y));        
          overlay.setMap(null); delete overlay;
          return LatLng;
      }
      

      很惊讶这不在google.maps.Map 对象中。

      【讨论】:

      • 这与之前的答案有何不同?
      • 清理简单的函数,不会在地图上留下叠加层。
      【解决方案3】:

      这是一个不依赖 OverlayView() 的代码。此方法利用地图 API 中内置的 fromPointToLatLng() 方法。

      function pixelToLatlng(xcoor, ycoor) {
        var ne = map.getBounds().getNorthEast();
        var sw = map.getBounds().getSouthWest();
        var projection = map.getProjection();
        var topRight = projection.fromLatLngToPoint(ne);
        var bottomLeft = projection.fromLatLngToPoint(sw);
        var scale = 1 << map.getZoom();
      
        var newLatlng = projection.fromPointToLatLng(new google.maps.Point(xcoor / scale + bottomLeft.x, ycoor / scale + topRight.y));
        return newLatlng;
      };
      

      来源: http://magicalrosebud.com/how-to-use-googlemaps-api-frompointtolatlng/

      这是一个小提琴:http://jsfiddle.net/mhaq865o/5/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-19
        相关资源
        最近更新 更多