从鼠标位置获取地图纬度经度 [英] get map latitude longitude from mouse position

查看:91
本文介绍了从鼠标位置获取地图纬度经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将鼠标在Google地图上的位置转换为LatLng对象。我看到了很多关于通过谷歌地图点击事件获得位置的帖子,如下所示:

I am trying to convert the position of the mouse on a google map into a LatLng object. I see quite a few posts on getting the position from with the google map "click" event etc, like this:

google.maps.event.addListener(map, 'click', function(event) {
  mouseLocation = event.latLng;
});

但是,这不适合我的目的,因为我没有回应地图事件,我'回应'tapHold'事件。在tapHold事件中,我想获取当前鼠标位置的经纬度。其实我可以看到这样的功能在很多方面都有用,而不仅仅是一个tapHold事件。

But, this doesn't work for my purposes because I am not responding to a map event, I'm responding to a 'tapHold' event. Inside the tapHold event I'd like to get the latitude and longitude of the current mouse position. Actually I could see a function like this being useful in many ways beyond just a tapHold event.

我可以想到一些攻击,例如,在飞行中创建一个mouseover事件,然后只让它触发一次,并在从rollover事件中获取LatLng对象后将其删除,但是,对我来说似乎有些ha ... ......寻找更优雅的解决方案。感谢!

I can think of some hacks like, to create a mouseover event on the fly, then only let it fire once and delete it after getting the LatLng object from the rollover event, but, seems sorta hackish to me... Looking for a more elegant solution. Thanks!

推荐答案

google.maps.event.addListener(map, 'mousemove', function (event) {
              displayCoordinates(event.latLng);               
          });

function displayCoordinates(pnt) {

          var lat = pnt.lat();
          lat = lat.toFixed(4);
          var lng = pnt.lng();
          lng = lng.toFixed(4);
          console.log("Latitude: " + lat + "  Longitude: " + lng);
      }

来源:http://seydahatipoglu.wordpress.com/2012/10/21/how-to-display-cursor -coordinates-in-google-map-javascript /

这篇关于从鼠标位置获取地图纬度经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆