Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop assuming lat and lng are in degree, minute, second #2597

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions app/javascript/controllers/geocode_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ export default class extends Controller {
this.getElevationTarget.disabled = true
}

// Using a regular expression
isValidDecimal(str) {
return /^-?\d*\.?\d+$/.test(str);
}

// Convert from human readable and do a rough check if they make sense
validateLatLngInputs(update = false) {
this.verbose("geocode:validateLatLngInputs")
Expand All @@ -331,17 +336,21 @@ export default class extends Controller {
origLng = this.lngInputTarget.value
let lat, lng

try {
let coords = convert(origLat + " " + origLng)
lat = coords.decimalLatitude,
lng = coords.decimalLongitude
}
// Toss any degree-minute-second notation and just take the first number
catch {
if (this.isValidDecimal(origLat) && this.isValidDecimal(origLng)) {
lat = parseFloat(origLat)
lng = parseFloat(origLng)
} else {
try {
let coords = convert(origLat + " " + origLng)
lat = coords.decimalLatitude,
lng = coords.decimalLongitude
}
// Toss any degree-minute-second notation and just take the first number
catch {
lat = parseFloat(origLat)
lng = parseFloat(origLng)
}
}

if (!lat || !lng)
return false
if (lat > 90 || lat < -90 || lng > 180 || lng < -180)
Expand Down
Loading