I am currently working on developing a functionality that can return an array of land-based coordinates. I have utilized Google's geocoder to create individual land-based coordinates successfully, but now I want to implement it in a way where an array of these coordinates is generated by continuously calling the function using a while loop until the desired number of coordinates is achieved. However, when attempting to execute this function in JSFiddle, the page crashes.
I cannot pinpoint the reason behind this issue as I had anticipated the reduction of coordinates each time a location-based coordinate is found. If no coordinate is found, the function should continue to be called until one is eventually located (which is expected to happen at some point in the future).
Any help or guidance on this matter would be greatly appreciated. You can access the public fiddle through this link: 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);
};
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
setTimeout(geocoder.geocode({
location: randomCoordinate
}, function (results, status) {
if (status == "OK" && results) {
var landCoordinate = randomCoordinate;
coords.push(landCoordinate);
number--;
}
}), 250);
}
}
return fn(coords);
}