生成从Google地图中心到另一个地图网站的链接

时间:2013-07-24 21:01:00

标签: php javascript google-maps html-form

使用Google Maps API v3,我想获取地图中心坐标,并根据它们构建一个URL,将用户发送到外部地图网站。

构建URL不是问题,但是在地图移动时保持URLs更新。我想知道是否可以在我的提交按钮上设置onClick事件,该事件会抓取当前的latlngzoom以及用户的网站想要去(从下拉菜单中选择。)。

我计划将这些变量推送到.php页面,该页面将获取变量,构建URL,然后重定向用户。

PHP部分和HTML表单部分是微不足道的 - 将变量放到php脚本上就是让我感到难过。

2 个答案:

答案 0 :(得分:0)

javascript-part不是那么简单,观察地图的idle - 事件并设置值的输入值(这些可能是隐藏的): 脚本:

 google.maps.event.addListener(mapObject, 'idle',function(){
        document.someForm.lat.value=this.getCenter().lat();
        document.someForm.lng.value=this.getCenter().lng();
        document.someForm.zoom.value=this.getZoom();
         });  

形式:

<form name="someForm">
  <input name="lat" /><input name="lng"/><input name="zoom"/>
</form>

但是,当你需要PHP脚本所需要的只是构建URL时,这也可以通过javascript轻松完成。

答案 1 :(得分:0)

为什么你要把lat,lng和zoom缩放到PHP页面呢?在javascript中本地构造URL。

Map对象具有getZoom和getCenter功能。

https://developers.google.com/maps/documentation/javascript/reference#Map

相关问题