使用地理编码返回陆基坐标数组时出现问题
Issue using geocoding to return array of land-based coordinates
我正在尝试创建一个功能来有效地返回陆基坐标数组。我已经使用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
}
可能会有所帮助?
相关文章:
- JavaScript/jQuery JSON数组问题-值为未定义
- array_push平面数组问题-需要能够向数组中添加多个变量
- 尽管使用了off,click函数仍会触发,这可能也会导致数组问题
- JavaScript/jQuery悬停函数的行为很有趣——数组问题
- 使用新值更新时出现数组问题
- Javascript 矩阵数组问题
- 对于javascript和jquery的循环数组问题
- 绘制 C3 图表时的 JSON 到数组问题
- Spring 3 中的字符串数组问题
- JavaScript 数组问题
- Javascript图像数组问题
- Javascript数组问题
- 使用JS的数组问题
- 简单的Javascript数组问题
- 在多维数组问题上用javascript实现foreach循环
- 合并并返回唯一数组问题
- Javascript关联数组问题
- ng-repeat声明中的数组问题
- d3.js数组问题:Uncaught TypeError: Cannot read property 'date
- Javascript数组问题:无法读取未定义属性