从静态谷歌地图获取经度和纬度

时间:2013-10-09 07:46:52

标签: google-maps google-static-maps

我想在地图上显示static map并鼠标悬停在地图上的鼠标上获取经度和经度

1 个答案:

答案 0 :(得分:5)

需要一些计算,你会在这个演示的源代码中找到所需的方法:https://developers.google.com/maps/documentation/javascript/examples/map-coordinates?csw=1

实现这些计算以使用静态映射的函数:

function FX(lat,//center-latitude of the static-map
            lng,//center-longitude of the static-map
            zoom,//zoom of the static-map
            width,//width of the static-map
            height,//height of the static-map
            mouseX,//x-coordinate of the mouseevent inside the element
            mouseY//y-coordinate of the mouseevent inside the element
            ){


   var x = mouseX-(width/2),
       y = mouseY-(height/2),
       s = Math.min(Math.max(Math.sin(lat * (Math.PI / 180)), -.9999), .9999),
       tiles = 1 << zoom,
       centerPoint={
                    x: 128 + lng * (256/ 360),
                    y: 128 + 0.5 * Math.log((1 + s) / (1 - s)) 
                       *-(256 / (2 * Math.PI))
                   },
       mousePoint={
                    x:(centerPoint.x*tiles)+x,
                    y:(centerPoint.y*tiles)+y
                  },
       mouseLatLng={
                    lat:(2 * Math.atan(Math.exp(((mousePoint.y/tiles) - 128) 
                                        / -(256/ (2 * Math.PI)))) -
                            Math.PI / 2)/ (Math.PI / 180),
                    lng:(((mousePoint.x/tiles) - 128) / (256 / 360))
                   };

      return mouseLatLng;

    }

演示:http://jsfiddle.net/doktormolle/yxf2C/show/