const isValidLocatorString = locatorString => locatorString.match(/^[A-Ra-r][A-Ra-r]\d\d[A-Xa-x][A-Xa-x]/) !== null; const charToNumber = char => char.toUpperCase().charCodeAt(0) - CHAR_CODE_OFFSET; const numberToChar = number => String.fromCharCode(number + CHAR_CODE_OFFSET); const CHAR_CODE_OFFSET = 65; const degToRad = deg => (deg % 360) * Math.PI / 180; const radToDeg = rad => (rad / Math.PI *180) % 360; const isValidPoint = (lat, lng) => (lat >= -90 && lat <= 90) && (lng >= -180 && lng <= 180); var clickmarkers = []; var clicklines = []; function ConvertDDToDMS(lat, lng) { var LatLng = []; if (lng < -180) { lng = lng + 360; } if (lng > 180) { lng = lng - 360; } LatLng['latDeg'] = (lat < 0 ? "S" : "N") + " " + pad((0 |(lat < 0 ? (lat = -lat) : lat)), 2) + "° " + pad(0 | (((lat += 1e-9) % 1) * 60),2) + "' " + ((0 | (((lat * 60) % 1) * 6000)) / 100) + "\""; LatLng['lngDeg'] = (lng < 0 ? "W" : "E") + " " + pad((0 | (lng < 0 ? (lng = -lng) : lng)), 3) + "° " + pad(0 | (((lng += 1e-9) % 1) * 60),2) + "' " + ((0 | (((lng * 60) % 1) * 6000)) / 100) + "\""; return LatLng; } function pad(n, width, z) { z = z || '0'; n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; } const latLngToLocator = (lat, lng) => { if (lng < -180) { lng = lng + 360; } if (lng > 180) { lng = lng - 360; } if (!isValidPoint(lat, lng)) { throw new Error('Input is not a valid coordinate'); } const longitude = lng + 180; const latitude = lat + 90; const fieldLng = numberToChar(Math.floor(longitude / 20)); const fieldLat = numberToChar(Math.floor(latitude / 10)); const squareLng = Math.floor(longitude % 20 / 2); const squareLat = Math.floor(latitude % 10); const subsquareLng = numberToChar(Math.floor((longitude % 20 % 2) * 12)).toLowerCase(); const subsquareLat = numberToChar((latitude % 10 - squareLat) * 24).toLowerCase(); return fieldLng + fieldLat + squareLng + squareLat + subsquareLng + subsquareLat; }; function onMapMove(event) { var LatLng = event.latlng; var lat = LatLng.lat; var lng = LatLng.lng; var LatLng2 = ConvertDDToDMS(lat, lng); $('#latDeg').html(LatLng2.latDeg); $('#lngDeg').html(LatLng2.lngDeg); var locator = latLngToLocator(lat,lng); $('#locator').html(locator); var distance = bearingDistance(homegrid, locator); $('#bearing').html(distance.deg + ' deg'); $('#distance').html(Math.round(distance.km * 10) / 10 + ' km'); }; function onMapClick(event) { var LatLng = event.latlng; var lat = LatLng.lat; var lng = LatLng.lng; var locator = latLngToLocator(lat,lng); var fromCoords = locatorToLatLng(homegrid); var marker = L.marker([fromCoords[0], fromCoords[1]], {closeOnClick: false, autoClose: false}).addTo(map).bindPopup(homegrid); clickmarkers.push(marker); var result = bearingDistance(homegrid, locator); var distance = Math.round(result.km * 10) / 10 + ' km'; var bearing = Math.round(result.deg * 10) / 10 + ' deg'; var popupmessage = '