最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't get google maps computeDistanceBetween() to return a value - Stack Overflow

programmeradmin8浏览0评论

The puteDistanceBetween() function in google maps geometry library will not return a value for me. Using the alert function it says the distance is "[object, Object]". Can anyone see where I'm going wrong? here are the important parts of the code in question:

<script type="text/javascript" src=";libraries=geometry"></script>
<script type="text/javascript" >

    var myArray1= [['location1', lat1, lng1], ['location2', lat2, lng2], ...];
    var myArray2= [['locationA', latA, lngA], ['locationB', latB, lngB], ...];
    var arrays = [myArray1, myArray2];

    function codeStart() {
      var orig;
      var startAddress = document.getElementById("start").value;
      geocoder.geocode( { 'address': startAddress}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var i = 0;
                var input = results[0].geometry.location;
                while (i < arrays.length) {             
                    orig = closest(input, arrays[i]);
                }
            }
      });
    }

    function closest(latlng, array){
      var distance;
      var c = 0;
      while (c < array.length){
          var location = array[c];
          var locationlatlng = new google.maps.LatLng(location[1],location[2]);
          distance = new google.maps.geometry.sphericalputeDistanceBetween(latlng, locationlatlng);
          alert(distance);  // popup box says "[object, Object]"
          c++;
      }
    }

</script>

The puteDistanceBetween() function in google maps geometry library will not return a value for me. Using the alert function it says the distance is "[object, Object]". Can anyone see where I'm going wrong? here are the important parts of the code in question:

<script type="text/javascript" src="http://maps.googleapis./maps/api/js?sensor=false&libraries=geometry"></script>
<script type="text/javascript" >

    var myArray1= [['location1', lat1, lng1], ['location2', lat2, lng2], ...];
    var myArray2= [['locationA', latA, lngA], ['locationB', latB, lngB], ...];
    var arrays = [myArray1, myArray2];

    function codeStart() {
      var orig;
      var startAddress = document.getElementById("start").value;
      geocoder.geocode( { 'address': startAddress}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var i = 0;
                var input = results[0].geometry.location;
                while (i < arrays.length) {             
                    orig = closest(input, arrays[i]);
                }
            }
      });
    }

    function closest(latlng, array){
      var distance;
      var c = 0;
      while (c < array.length){
          var location = array[c];
          var locationlatlng = new google.maps.LatLng(location[1],location[2]);
          distance = new google.maps.geometry.spherical.puteDistanceBetween(latlng, locationlatlng);
          alert(distance);  // popup box says "[object, Object]"
          c++;
      }
    }

</script>
Share Improve this question edited Jul 25, 2011 at 1:54 Trott 70.1k27 gold badges181 silver badges217 bronze badges asked Jul 25, 2011 at 1:32 slapeeslapee 831 gold badge1 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

puteDistanceBetween is a static method. So this line:

distance = new google.maps.geometry.spherical.puteDistanceBetween(latlng, locationlatlng);

should be this instead:

distance = google.maps.geometry.spherical.puteDistanceBetween(latlng, locationlatlng)

By the way, when alert() tells you that something is an object, it's a good time to switch to console.dir() instead of alert() so you can (at least in some browsers) look at the contents of the object in the console/dev tools. If you don't know much about your JavaScript console, check it out. It will save you tons of time.

distance = new google.maps.geometry.spherical.puteDistanceBetween(latlng, locationlatlng);

For some reason you've used the syntax to create a new object. That is why, when you alert(distance), you're seeing that distance is an object.

puteDistanceBetween is just a function:

distance = google.maps.geometry.spherical.puteDistanceBetween(latlng, locationlatlng);
发布评论

评论列表(0)

  1. 暂无评论