【问题标题】:Get latitude and longitude from static google map从静态谷歌地图获取纬度和经度
【发布时间】:2013-10-09 07:46:52
【问题描述】:

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

【问题讨论】:

  • 我使用以下方法创建了静态地图 maps.googleapis.com/maps/api/staticmap?center=8.4875, 76.95258&zoom=8&size=500x500&sensor=false">。现在这是一张图片,鼠标悬停在我想要获取 lat 和要在警报框中显示的长值。
  • 当您尝试使用交互式地图绘制它时发生了什么?
  • 使用 geochart 时,我无法为省/州以外的某些地区着色
  • Raji,我真的很想帮忙,但你没有很好地解释你的问题。尝试阅读 thisthis 以了解如何提出好问题并尝试 edit 您的帖子以使其符合这些建议。

标签: google-maps google-static-maps


【解决方案1】:

它需要一些计算,你会在这个演示的源代码中找到所需的方法: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: jsfiddle.net/doktormolle/8L33fhd6/show 函数FY 返回一个具有属性point(包含x/y 像素坐标)和inRange(它是true)的对象当像素在 image/staticMap 内时,否则false)
  • 感谢您的回复并提供解决方案,我会查看并回复您。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2019-03-29
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多