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

5

我希望显示一个静态地图,在鼠标悬停在地图上时获取经度和纬度。


我使用以下代码创建了静态地图:<img src="http://maps.googleapis.com/maps/api/staticmap?center=8.4875, 76.95258&zoom=8&size=500x500&sensor=false">。现在这是一张图片,在鼠标悬停时,我想要获取经纬度值并在警告框中显示。 - Raji A C
当您尝试使用交互式地理图绘制它时发生了什么事情? - jmac
当使用地理图表时,我无法为除省/州以外的某些地区着色。 - Raji A C
Raji,我真的想帮忙,但你没有很好地解释你的问题。尝试阅读这个这个,以获得如何提出一个好问题的好主意,并尝试[编辑]你的帖子,使其符合这些建议。 - jmac
1个回答

6

这需要一些计算,你可以在此演示的源代码中找到所需的方法: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/

@Avinash Agrawal:http://jsfiddle.net/doktormolle/8L33fhd6/show/ 函数 FY 返回一个对象,该对象具有属性 point(包含 x/y 像素坐标)和 inRange(当像素在图像/静态地图内时为 true,否则为 false)。 - Dr.Molle
谢谢您的回复和提供解决方案,我会查看并回复您。再次感谢。 - Avinash Agrawal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接