谷歌地图api v2地址经纬度问题

发布于 2024-12-29 00:48:09 字数 1971 浏览 0 评论 0原文

到目前为止,我有一个有效的秘密脚本可以从地址获取经度和纬度。 问题是我无法提交,而提交按钮用于获取经度和纬度。

所以我想要以下内容: 地址将被放置在隐藏字段中。 (由我的 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>

I have so far a working covert script to get Longitude and Latitude from an Address.
Problem is that I can't submit, while the submit button is used to get the Longitude and Latitude.

So I would like the following:
Address will be placed in a hidden field. (populated by my CMS, ExpressionEngine)
Than on page load the Longitude and Latitude will be collected in two input fields
than press submit and done.

But how to do this, I use the following code so far.
Please help.

     <!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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-01-05 00:48:09

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

    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 地图上显示结果。请参阅 10.1.1(g)
  2. 您不应该再使用 v2 进行开发,它已被弃用并且很快就会消失。 改用 v3
  3. 您可以使用网络服务地理编码 API 来完成这一切服务器,并完全避免这种复杂性。

By the time you're submitting the form, it's too late to make a request to geocode an address.
It's better to geocode the address on the page load, and then just submit the form.

    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;
            }
          }
        );
      }
    } 

By the way, three important points:

  1. It's against Google's Terms of Service to geocode without displaying the results on a Google Map. See 10.1.1(g)
  2. You shouldn't be developing with v2 anymore, it's deprecated and going away soon. Use v3 instead.
  3. There is a web service geocoding API you can use to do this all in the server, and avoid this complexity altogether.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文