【问题标题】:Google maps api v2 Longitude and Latitude of address issueGoogle maps api v2 地址问题的经度和纬度
【发布时间】:2012-01-24 08:41:58
【问题描述】:

到目前为止,我有一个可以从地址获取经度和纬度的隐蔽脚本。 问题是我不能提交,而提交按钮是用来获取经度和纬度的。

所以我想要以下内容: 地址将被放置在隐藏字段中。 (由我的 CMS、ExpressionEngine 填充) 比页面加载时,经度和纬度将收集在两个输入字段中 而不是按提交并完成。

但是如何做到这一点,我到目前为止使用以下代码。 请帮忙。

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Longitude and Latitude from address</title>

        <script src="http://maps.google.com/maps?file=api&v=2&key=YOUR KEY" type="text/javascript" charset="utf-8">

         </script>
            <script type="text/javascript">
        //<![CDATA[

        function load() {
          if (GBrowserIsCompatible()) {
            geocoder = new GClientGeocoder();
           }
        }

        function showAddress(geolocation) {
          if (geocoder) {
            geocoder.getLatLng(
              geolocation,
              function(point) {
                if (!point) {
                  alert(geolocation + " not found");
                } else {
                  document.geoform.longitude.value = point.x;
                  document.geoform.latitude.value = point.y;
                }
              }
            );
          }
        } 

        //]]>
        </script>
        </head>
      <body onload="load()">

     <form action="#" name="geoform" id="geoform" onsubmit="showAddress(this.geolocation.value); return false">
     <input  type="hidden" name="geolocation" value="Address goes here, populated by cms" size="50"  />

     Decimal Longitude:
     <input  name="longitude" type="text" id="longitude" value="" />

     Decimal Latitude:
     <input  name="latitude" type="text"  id="latitude"  value="" />

     <input type="submit" value="Longitude/latitude codes" />
     </form>
     </body>
     </html>

【问题讨论】:

  • 你好矩阵,我没有收到错误消息。如果我按原样使用上面的脚本,我会得到 long 和 lat。如果我从表单中删除“return false”,它正在提交但没有得到 long 和 lat。这就是为什么我想在页面加载时使用。 (我试过 .submit() 但没有任何运气)

标签: javascript latitude-longitude google-maps-api-2


【解决方案1】:

在您提交表单时,请求对地址进行地理编码已经太晚了。 最好在页面加载时对地址进行地理编码,然后提交表单。

    function load() {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(
          document.geoform.geolocation.value,
          function(point) {
            if (!point) {
              alert(geolocation + " not found");
            } else {
              document.geoform.longitude.value = point.x;
              document.geoform.latitude.value = point.y;
            }
          }
        );
      }
    } 

顺便说一下重要的三点:

  1. 在不将结果显示在 Google 地图上的情况下进行地理编码违反 Google 的 Terms of Service。请参阅 10.1.1(g)
  2. 您不应该再使用 v2 进行开发,它已被弃用并且很快就会消失。 Use v3 instead
  3. 您可以使用web service geocoding API 在服务器中完成这一切,并完全避免这种复杂性。

【讨论】:

    猜你喜欢
    • 2013-02-23
    • 2013-04-02
    • 2013-03-20
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多