写一个位置并获取地线

写一个位置并获取地线

本文介绍了写一个位置并获取地线,不能正常工作? JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接已删除

这是我的网站.我正在努力,因此您可以在搜索输入字段中输入位置,然后查看距离餐厅xx公里的地方.

This is my site. Im working on so you can enter a location on the search input field, and then see theres xx km from a restaurant.

现在我几乎可以正常工作了.如果您看到源代码,则可以看到它是如何工作的.它显示地址(您搜索的内容).然后显示地址将其放入lat/lng线,然后将其传递到computeRestaurants(),后者计算您输入的位置与餐厅之间的距离.

Now i got almost everything working. If you see the source code you can see how it works. It showAddress(what you searched). And then show addresses makes it into lat/lng cords, and pass it to computeRestaurants() which computes the distances between the location you entered and the restaurants.

不知何故,当我跑步时:

Somehow, when I run:

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

在功能之外,它可以正常工作并提供正确的值.

outside the functions, it works and gives correct values.

但是当我这样做时:

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

您可以看到它返回NaN.在showAddress()内部,它执行与我在computeRestaurants(point)上面编写的命令相同的命令

You can see that it returns NaN. And inside showAddress() it executes the same command as the one i wrote above computeRestaurants( the point )

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

So why will it not work properly?

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

point in showAddress is: (55.662133, 12.508028) so it is already converted to LatLng cords and therefore no need to new google.maps.latlng(...

我目前唯一的选择是方括号()??

My only bet right now is the brackets () ??

推荐答案

以此替换您的showAddress:

replace your showAddress by this:

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 api的混合,这就是问题所在.

You are using mixup of v2 and v3 apis, and that is the problem.

这篇关于写一个位置并获取地线,不能正常工作? JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:07