Write a location and get the geocords, not working properly? JS

被刻印的时光 ゝ 提交于 2019-12-12 05:48:19

问题


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:


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.



来源:https://stackoverflow.com/questions/7866060/write-a-location-and-get-the-geocords-not-working-properly-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!