写一个位置并获取geocords,不能正常工作? JS

时间:2011-10-23 12:13:33

标签: google-maps geocode google-geocoder

已删除链接

这是我的网站。我正在努力,你可以在搜索输入字段中输入一个位置,然后从餐馆看到距离xx km。

现在几乎所有的东西都在工作。如果您看到源代码,您可以看到它是如何工作的。它显示地址(您搜索的内容)。然后显示地址将其转换为lat / lng线,并将其传递给computeRestaurants(),它计算您输入的位置与餐馆之间的距离。

不知何故,当我跑:

computeRestaurants(new google.maps.LatLng(55.662133, 12.508028)); 

在函数之外,它可以工作并给出正确的值。

但是当我这样做时:

showAddress('Valby'); // (like in the source code)

你可以看到它返回NaN。在showAddress()里面,它执行的命令与我在computeRestaurants(点)上面写的命令相同(

那为什么它不能正常工作?

showAddress中的

point为:(55.662133, 12.508028)因此它已经转换为LatLng线,因此无需new google.maps.latlng(...

我现在唯一的赌注是方括号()??

1 个答案:

答案 0 :(得分:1)

用这个替换你的showAddress:

var geocoder;
function showAddress(address)
{
  if (typeof(geocoder) == 'undefined') geocoder = new google.maps.Geocoder();

  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      computeRestaurants(results[0].geometry.location);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

你正在使用v2和v3 apis的混合,这就是问题所在。

相关问题