2012-01-24 177 views
0

我到目前为止一个工作秘密脚本从一个地址得到经度和纬度。 问题是我无法提交,而提交按钮用于获取经度和纬度。谷歌地图api v2经度和纬度的地址问题

所以我想下面: 地址将被放置在一个隐藏的领域。 (由我的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> 
+0

你会得到什么错误? – Unknown

+0

Hello Matrix,我没有收到错误。如果我使用上面的脚本,我会得到长和经。如果我从表单中删除“返回false”,它会提交,但不会获得长和经。这就是我想在页面加载时使用的原因。 (我试过.submit()但没有任何运气) –

回答

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. 这是对谷歌的Terms of Service不显示在谷歌地图的结果进行地理编码。见10.1.1(g)
  2. 你不应该再用v2进行开发,它已经被弃用并且很快就会消失。 Use v3 instead
  3. 您可以使用一个web service geocoding API在服务器中完成这一切,并完全避免这种复杂性。
相关问题