/* Most of the code here is straight out of Google's example code at https://developers.google.com/maps/documentation/javascript/examples/marker-remove */ 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>'; var url2 = 'http://tiger.hpc.okstate.edu/thredds/ncss/grid/agg-soil-moisture/agg_vwc.nc?var=vwc&temporal=all&latitude='+lat+'&longitude='+lon+'&accept=geocsv_file'; //var htmlstring2 = '<br/><a href="'+url2+'">'+url2+'</a>'; var htmlstring2 = '<br/><a href="'+url2+'">'+url2+'</a>'; document.getElementById("download-url").innerHTML = htmlstring2; } window.onload = function () { initMap() };