Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Created May 19, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jacajack/9c3e7d6c0d49791e87e7c7ce1411c09d to your computer and use it in GitHub Desktop.
Save Jacajack/9c3e7d6c0d49791e87e7c7ce1411c09d to your computer and use it in GitHub Desktop.
Random trip destinations
<!DOCTYPE html>
<html style="height: 100%;">
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=&libraries=geometry&sensor=false&ext=.js"></script>
<script>
var circle;
var infowindow = new google.maps.InfoWindow({});
function initialize() {
var map = new google.maps.Map(document.getElementById("map"),
{
zoom: 4,
center: new google.maps.LatLng(50.182, 18.894),
mapTypeId: google.maps.MapTypeId.HYBRID
});
circle = new google.maps.Circle({
center: map.getCenter(),
radius: 20e3, // meters
strokeColor: "#0000FF",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#0000FF",
fillOpacity: 0.05
});
circle.setMap(map);
var bounds = circle.getBounds();
map.fitBounds(bounds);
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
for (var i = 0; i < 25; i++) {
var ptLat = Math.random() * (ne.lat() - sw.lat()) + sw.lat();
var ptLng = Math.random() * (ne.lng() - sw.lng()) + sw.lng();
var point = new google.maps.LatLng(ptLat,ptLng);
if (google.maps.geometry.spherical.computeDistanceBetween(point,circle.getCenter()) < circle.getRadius()) {
createMarker(map, point,"marker "+i);
// break;
}
}
}
function createMarker(map, point, content) {
var marker = new google.maps.Marker({position:point, map:map});
google.maps.event.addListener(marker, "click", function(evt) {
infowindow.setContent(content+"<br>"+marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
});
return marker;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body style="height: 100%;">
<div id="map" style="width: 100%; height: 100%;">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment