Important Note: The solution presented below utilizes pure vanilla Javascript.
Without access to a database, you can utilize an array of LatLng class to define the coordinates for display purposes:
//an array representing geographical coordinates
var locations = [
new google.maps.LatLng(6.528166, 20.439864), //location in Africa
new google.maps.LatLng(34.494890, 103.854720), //location in China
new google.maps.LatLng(51.802090, 7.771800), // location in Germany
new google.maps.LatLng(46.153450, -101.948783), // location in US
new google.maps.LatLng(-11.495951, -49.266451), // location in Brazil
new google.maps.LatLng(-80.287421, 23.599101) // location in Antartica
];
Subsequently, the setInterval()
function can be employed to randomize items within the geographical coordinates array and alter both the map and marker positions.
// set interval to repeat every 5 seconds
setInterval(function() {
// select random coordinates from the locations array
var randomLocation = locations[Math.floor(Math.random() * locations.length)];
// update the map center
map.setCenter(randomLocation);
// update the marker position
marker.setPosition(randomLocation);
}, 5000);
A live demonstration of the functioning solution can be found here.
Trust this clarifies any queries!