Skip to content

Instantly share code, notes, and snippets.

@rdmpage
Created April 2, 2014 13:55
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 rdmpage/9934629 to your computer and use it in GitHub Desktop.
Save rdmpage/9934629 to your computer and use it in GitHub Desktop.
Edit point location
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Edit point location</title>
<style type="text/css">
body {
margin: 0;
padding: 40px;
font-family: sans-serif;
}
#map {
width: 100%;
height: 300px;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var MapEditor = function (lat, long, radius) {
var map;
var marker = null;
var circle = null;
var precision = radius;
google.load('maps', '3', {
other_params: 'sensor=false'
});
google.setOnLoadCallback(initialize);
//------------------------------------------------------------------------------------------
function update_latitude(decimal_latitude) {
$("#latitude_decimal").val(decimal_latitude);
decimalToDms(decimal_latitude, true);
}
//------------------------------------------------------------------------------------------
function update_longitude(decimal_longitude) {
$("#longitude_decimal").val(decimal_longitude);
decimalToDms(decimal_longitude, false);
}
//------------------------------------------------------------------------------------------
// http://stackoverflow.com/questions/5786025/decimal-degrees-to-degrees-minutes-and-seconds-in-javascript
function decimalToDms(decimal, latitude) {
var hemisphere = '';
if (latitude) {
hemisphere = decimal > 0 ? 'N' : 'S';
} else {
hemisphere = decimal > 0 ? 'E' : 'W';
}
var degrees = 0 | Math.abs(decimal);
var minutes = 0 | Math.abs(decimal) % 1 * 60;
var seconds = 0 | Math.round((Math.abs(decimal) % 1 * 60 - minutes) * 60);
// handle rounding
if (seconds == 60) {
minutes++;
seconds = 0;
}
if (minutes == 60) {
degrees++;
minutes = 0;
}
if (latitude) {
$("#latitude_degrees").val(degrees);
$("#latitude_minutes").val(minutes);
$("#latitude_seconds").val(seconds);
$("#latitude_hemisphere").val(hemisphere);
} else {
$("#longitude_degrees").val(degrees);
$("#longitude_minutes").val(minutes);
$("#longitude_seconds").val(seconds);
$("#longitude_hemisphere").val(hemisphere);
}
}
//------------------------------------------------------------------------------------------
function dmsToDecimal(degrees, minutes, seconds, hemisphere) {
var decimal = degrees + minutes/60 + seconds/3600;
if (hemisphere == 'S') {
decimal *= -1;
}
if (hemisphere == 'W') {
decimal *= -1;
}
return decimal;
}
//------------------------------------------------------------------------------------------
function initialize() {
var center = new google.maps.LatLng(lat, long);
update_latitude(center.lat());
update_longitude(center.lng());
$("#latitude_longitude_precision").val(precision);
map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: center,
mapTypeId: google.maps.MapTypeId.TERRAIN,
draggableCursor: 'auto',
scaleControl: true
});
circle = new google.maps.Circle({
strokeColor: "#882A28",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF6963",
fillOpacity: 0.3,
editable: true,
center: center,
map: map,
radius:precision
});
marker = new google.maps.Marker(
{
map:map,
draggable:true,
animation: google.maps.Animation.DROP,
position: center
});
// User has moved marker
google.maps.event.addListener(marker, 'dragend', function()
{
var pos = this.getPosition();
update_latitude(pos.lat());
update_longitude(pos.lng());
circle.setCenter(pos);
});
// User has moved circle
google.maps.event.addListener(circle, 'center_changed', function()
{
var pos = this.getCenter();
update_latitude(pos.lat());
update_longitude(pos.lng());
marker.setPosition(pos);
});
// User has changed circle size
google.maps.event.addListener(circle, 'radius_changed', function()
{
var precision = Math.floor(this.getRadius());
$("#latitude_longitude_precision").val(precision);
});
// Handle text inputs
$('#latitude_longitude_precision').blur(function() {
var precision = parseInt($("#latitude_longitude_precision").val());
// Make sure this a sensible number, ignore but inputs
if (isNaN(precision)) {
// not a number, ignore
precision = 10000;
} else {
if ((precision < 0) || (precision > 100000)) {
precision = 10000;
}
}
$("#latitude_longitude_precision").val(precision);
circle.setRadius(precision);
});
$('#latitude_decimal').blur(function() {
var currentPos = marker.getPosition();
var decimal_latitude = $("#latitude_decimal").val();
// Make sure this a sensible number, ignore but inputs
if (isNaN(decimal_latitude)) {
// not a number, ignore
decimal_latitude = currentPos.lat();
} else {
if ((decimal_latitude < -90) || (decimal_latitude > 90)) {
decimal_latitude = currentPos.lat();
}
}
if (decimal_latitude == currentPos.lat()) {
$("#latitude_decimal").val(decimal_latitude);
} else {
decimalToDms(decimal_latitude, true);
var newPos = new google.maps.LatLng(decimal_latitude, currentPos.lng());
circle.setCenter(newPos);
map.setCenter(newPos);
}
});
$('#longitude_decimal').blur(function() {
var currentPos = marker.getPosition();
var decimal_longitude = $("#longitude_decimal").val();
// Make sure this a sensible number, ignore but inputs
if (isNaN(decimal_longitude)) {
// not a number, ignore
decimal_longitude = currentPos.lng();
} else {
if ((decimal_longitude < -180) || (decimal_longitude > 180)) {
decimal_longitude = currentPos.lng();
}
}
if (decimal_longitude == currentPos.lng()) {
$("#longitude_decimal").val(decimal_longitude);
} else {
decimalToDms(decimal_longitude);
var newPos = new google.maps.LatLng(currentPos.lat(), decimal_longitude);
circle.setCenter(newPos);
map.setCenter(newPos);
}
});
}
};
</script>
</head>
<body>
<h1>Edit point location</h1>
<p>UI to edit a point location (inspired by <a href="http://www.inaturalist.org">iNaturalist observation editor</a>).</p>
<p style="color:rgb(192,192,192);">Click and drag either the marker or the centre of the circle to move point,
change size of circle to adjust precision, or enter values in text fields.</p>
<div>
<div id="map"></div>
<div>
<p>Latitude</p>
<!-- decimal -->
<input id="latitude_decimal" type="text" />
<!-- degrees, minutes, seconds -->
<input id="latitude_degrees" type="text" maxlength="2" size="4" readonly=""/>
<span>&deg;</span>
<input id="latitude_minutes" type="text" maxlength="2" size="2" readonly=""/>
<span>'</span>
<input id="latitude_seconds" type="text" maxlength="2" size="2" readonly=""/>
<span>''</span>
<select id="latitude_hemisphere" disabled="">
<option label="N" value="N"></option>
<option label="S" value="S"></option>
</select>
</div>
<div>
<p>Longitude</p>
<!-- decimal -->
<input id="longitude_decimal" type="text" />
<!-- degrees, minutes, seconds -->
<input id="longitude_degrees" type="text" maxlength="3" size="4" readonly=""/>
<span>&deg;</span>
<input id="longitude_minutes" type="text" maxlength="2" size="2" readonly=""/>
<span>'</span>
<input id="longitude_seconds" type="text" maxlength="2" size="2" readonly=""/>
<span>''</span>
<select id="longitude_hemisphere" disabled="">
<option label="W" value="W"></option>
<option label="E" value="E"></option>
</select>
</div>
<div>
<p>Precision (m)</p>
<input id="latitude_longitude_precision" type="text" />
</div>
</div>
<script>
var map = new MapEditor(-21.10,14.35, 7000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment