You may want to try the following:
function getRandom_marker(bounds) {
var lat_min = bounds.getSouthWest().lat(),
lat_range = bounds.getNorthEast().lat() - lat_min,
lng_min = bounds.getSouthWest().lng(),
lng_range = bounds.getNorthEast().lng() - lng_min;
return new google.maps.LatLng(lat_min + (Math.random() * lat_range),
lng_min + (Math.random() * lng_range));
}
Complete example:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps Random Points Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px;"></div>
<script type="text/javascript">
function getRandom_marker(bounds) {
var lat_min = bounds.getSouthWest().lat(),
lat_range = bounds.getNorthEast().lat() - lat_min,
lng_min = bounds.getSouthWest().lng(),
lng_range = bounds.getNorthEast().lng() - lng_min;
return new google.maps.LatLng(lat_min + (Math.random() * lat_range),
lng_min + (Math.random() * lng_range));
}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addListener(map, 'tilesloaded', function () {
var mapBounds = map.getBounds();
for (var i = 0; i < 20; i++) {
new google.maps.Marker({
position: getRandom_marker(mapBounds),
map: map
});
}
});
</script>
</body>
Screenshot: