谷歌地图地理编码到latlng

时间:2012-03-16 18:17:59

标签: javascript google-maps geocode

我试图从这个地理编码中获取标记的纬度和经度..这对你们来说似乎很简单。对不起,我尝试了很多像addListener这样的东西,但似乎没什么用。请帮忙:(

    function codeAddress() {
      var address = document.getElementById("address").value;
      geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }

2 个答案:

答案 0 :(得分:0)

我对地理编码器也有一些问题。 此代码适用于不包含特殊字符的地址:

var addr="YOUR ADDRES";  
 var geocoder = new google.maps.Geocoder();
     geocoder.geocode({address: addr, region: 'ar'}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
     var GeoPos=results[0].geometry.location;
                                                 }
                                                                             });

检查地理编码功能中的region参数。我正在使用ar,因为我来自阿根廷。

答案 1 :(得分:0)

这是一个代码,我可以用来在使用地理编码进行搜索时显示我的网站的纬度和经度...它显示在一个而不是一个文本框中,当我想到它时,它实际上更好。< / p>

var searchlat = new Number(results[0].geometry.location.lat());
var searchlng = new Number(results[0].geometry.location.lng());
document.getElementById('sethomelat').innerHTML=searchlat.toFixed(8)
document.getElementById('sethomelng').innerHTML=searchlng.toFixed(8)

.toFixed()让我指定将显示的确切小数位数。

希望这可以帮助那些遇到同样问题的人。 :d

相关问题