使用地理编码返回陆基坐标数组时出现问题

Issue using geocoding to return array of land-based coordinates

本文关键字:数组 问题 坐标 陆基 编码 返回      更新时间:2023-09-26

我正在尝试创建一个功能来有效地返回陆基坐标数组。我已经使用Google的地理编码器有效地创建了一次性的陆基坐标,但是我想将其集成到一个系统中,该系统最终通过使用while块创建陆基坐标数组,因此调用该函数直到确定必要数量的陆基坐标。我认为以下内容应该有效,但是,当调用该函数时,页面(使用 JSFiddle)崩溃。

我不知道为什么会这样,因为我相信每次找到基于位置的坐标时都应该减少数量,如果找不到,则会调用该函数,直到找到一个(应该在将来的某个时候)。

任何指导将不胜感激。公众小提琴在 http://jsfiddle.net/grabbeh/sajfb/

 var map;
 var coords = [];
 var geocoder = new google.maps.Geocoder();
window.onload = function () {
   center = new google.maps.LatLng(40.717, -74.006);
    var options = {
    zoom: 1,
    center: center,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
  map = new google.maps.Map(document.getElementById('map'), options);
  //randomLandBasedCoords(2, function(coords){
  // addMarkersToMap(coords);
  //});
  };
function addMarkersToMap(places) {
  for (var i = 0, j = places.length; i < j; i++) {
     placeMarker(places[i]);
     };
};
function placeMarker(location) {
 var marker = new google.maps.Marker({
    position: location,
    map: map,
    flat: true,
   });
};
function randomLandBasedCoords(number, fn) {
  if (number > 0) {
    while (number > 0) {
        // create random coordinate
        var lng = (Math.random() * 360 - 180);
        var lat = (Math.random() * 180 - 90);
        var randomCoordinate = new google.maps.LatLng(lat, lng);
        // call geocoder function to check if coordinate is land-based with a timeout    to control flow (maybe)
        setTimeout(geocoder.geocode({
            location: randomCoordinate
            }, function (results, status) {
               if (status == "OK" && results) {
                  var landCoordinate = randomCoordinate;
                  coords.push(landCoordinate);
                  number--;
               }
           }), 250);
       }
   }
   return fn(coords);
}

试试这个:

function randomLandBasedCoords(number,fn) {
    // create random coordinate
    var lng = (Math.random() * 360 - 180);
    var lat = (Math.random() * 180 - 90);
    var randomCoordinate = new google.maps.LatLng(lat, lng);
    // call geocoder function to check if coordinate is land-based
    geocoder.geocode({
        location: randomCoordinate
    }, function (results, status) {
        if (status == "ZERO_RESULTS") {
            randomLandBasedCoords(number,fn);
        } else if (status == "OK" && results) {
            var landCoordinate = randomCoordinate;
            coords.push(landCoordinate);
            if (coords.length < number) {
                randomLandBasedCoords(number,fn);
            } else {
                return fn(coords);
            }
        }
    });
}

当您执行"//调用地理编码器函数以检查坐标是否基于陆地"函数时,代码似乎正在丢失。

错误:

Error: useless setTimeout call (missing quotes around argument?)
}), 250);

现在没有时间调试它,但这是我过去成功使用的示例,看起来更简单:

if (status == google.maps.GeocoderStatus.OK) { // -- Has a result been returned ?
    DO process stuff
}else{ // -- Location not returned - must be in the sea !
    DO in the sea stuff
}

可能会有所帮助?