var map;
var markers = [];
function initMap() {
var osu = {lat: 36.12242, lng: -97.06967};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: osu,
mapTypeId: 'terrain'
});
var oklahoma = new google.maps.Polyline({
path: ok_polygon_coords,
geodesic: true,
strokeColor: '#FF7300',
strokeOpacity: 1.0,
strokeWeight: 2
});
oklahoma.setMap(map);
// This event listener will call addMarker() when the map is clicked.
map.addListener('click', function(event) {
// only do anything for points clicked inside Oklahoma
if(google.maps.geometry.poly.containsLocation(event.latLng, oklahoma)) {
deleteMarkers(); // get rid of the existing marker
addMarker(event.latLng); // add a new marker at the clicked point
var lat = event.latLng.lat().toFixed(5);
var lon = event.latLng.lng().toFixed(5);
document.getElementById("lat").innerHTML = lat;
document.getElementById("lon").innerHTML = lon;
update_download_url(lat,lon);
}
});
// Adds a marker at the center of the map.
addMarker(osu);
document.getElementById("lat").innerHTML = osu.lat;
document.getElementById("lon").innerHTML = osu.lng;
update_download_url(osu.lat,osu.lng);
}
// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push(marker);
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
function update_download_url(lat,lon) {
var url = 'http://soilmapnik.hpc.okstate.edu/'+lat+'/'+lon+'/vwc/data_request.csv';
var htmlstring = '<a href="'+url+'">'+url+'</a>';
document.getElementById("download-url").innerHTML = htmlstring;
}
window.onload = function () { initMap() };