简体   繁体   English

Google Maps-提取地图中心的坐标?

[英]Google Maps - Extract coordinates of the map's center?

I'm hoping to use a Google Map in this way: User zooms in on the location they want (using either pan & zoom or a search bar above the map) and the coordinates of the center of the map are passed down to my PHP script as either "$lat" & "$lon" OR "$coords". 我希望以这种方式使用Google地图:用户放大所需的位置(使用平移和缩放或地图上方的搜索栏),并将地图中心的坐标传递到我的PHP脚本为“ $ lat”和“ $ lon”或“ $ coords”。 If someone could find or write some code that does this I'd be elated... Let me know if more info is needed... My understanding of JavaScript (and even PHP) is limited so please try to be as complete as you can :) 如果有人可以找到或编写一些实现此目的的代码,我会很高兴...如果需要更多信息,请告诉我...我对JavaScript(甚至PHP)的理解是有限的,因此请尝试与您一样完整能够 :)

So, this is going to be problematic (and virtually impossible) because the PHP code is executed server side, before the page finishes loading in the client's browser. 因此,这将是有问题的(几乎是不可能的),因为在页面完成在客户端浏览器中的加载之前,PHP代码是在服务器端执行的。 Javascript is executed "live" as the user does things in the browser, so there really isn't an easy straightforward way to push from JS to PHP. 当用户在浏览器中执行操作时,JavaScript是“实时”执行的,因此,实际上没有简单易行的方法将JS推送到PHP。

To properly answer the question we need to know what you want to do with the coordinate data, and where this is the whole "framework" in your app, ex: 为了正确回答这个问题,我们需要知道您想对坐标数据做什么,以及这是应用程序中整个“框架”的位置,例如:

  • Do users pick a location and then send it to you and that's it? 用户会选择一个位置,然后将其发送给您吗?
  • Do they pick several locations (ie. pushpins or something) one after another (something like 'my favorite places') 他们是否一个接一个地挑选了几个地点(例如图钉等)(例如“我最喜欢的地方”)
  • Can you write to a DB and process the locations when they're done? 完成后,您可以写入数据库并处理位置吗?

You have a few options (ordered simplest to hardest) 您有几种选择(从最简单到最困难的顺序排列)

  1. Have the user pan/zoom the map to the spot they want, then click some button and, at that point, pull the coordinates from the map and push it to some PHP script (via GET or POST) 让用户将地图平移/缩放到所需位置,然后单击一些按钮,然后从地图上拉出坐标并将其推到某些PHP脚本(通过GET或POST)
  2. Write to a database - and include a "done" button that launches a PHP script to process the data. 写入数据库-并包含一个“完成”按钮,该按钮启动PHP脚本来处理数据。
  3. Let the user move around the map, and when they finish moving start up a little timer and refresh the page if they hold over that position for X amount of time. 让用户在地图上四处移动,完成移动后,启动一个小计时器,并在该位置保持X倍的时间刷新页面。 When you refresh the page, pass the coordinates back into the same page (via GET or POST). 刷新页面时,将坐标传递回同一页面(通过GET或POST)。 When you get it in PHP, do whatever you need to with the data (and recenter the map on the coordinate. This would be very annoying from a user's perspective. 在PHP中获取数据后,您需要对数据做任何事情(并在坐标上更新地图。从用户的角度来看,这将非常烦人。

Since you asked, you can get the map's center location by doing map.getCenter() , which returns a lat/lon pair 如您map.getCenter() ,您可以通过执行map.getCenter()来获取地图的中心位置,该方法会返回map.getCenter()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM