Write a location and get the geocords, not working

2019-09-17 13:24发布

Link removed

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.

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.

But when i do:

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

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 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 () ??

1条回答
甜甜的少女心
2楼-- · 2019-09-17 14:07

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

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

查看更多
登录 后发表回答