pure-maps icon indicating copy to clipboard operation
pure-maps copied to clipboard

allow search to input geographical coordinate format

Open Danfro opened this issue 6 months ago • 2 comments

Unless I am doing something wrong, it seems that search only allows to enter decimal coordinates for a search like 12.3456°. Sometimes we get coordinates in geographical format like 12°34'56.78". It would be nice if we could search for coordinates in this format too.

Danfro avatar Aug 17 '25 15:08 Danfro

Indeed, it would be nice.

rinigus avatar Aug 17 '25 15:08 rinigus

I hacked together a solution qml side only. Blame me using AI, because I am no good with regex. Took some iterations. 😀

If the search text's onTextChanged here is done like this:

onTextChanged: {
                var newText = searchField.text.trim();
                if (!newText && selection) selection = null;
                if (!newText && searchResults) searchResults = [];
                if (newText === searchField.prevText) return;
                selection = null;
                geo._searchPending = false;
                geo._searchDone = false;

                // Check if the input is a pair of geographical coordinates
                var decimalCoordinates = convertToDecimalCoordinates(newText);
                if (decimalCoordinates !== null) {
                    geo.query = decimalCoordinates.join(",");
                } else {
                    geo.query = newText;
                }

                searchField.prevText = newText;
                geo.update();
            }

and we add a function like this:

function convertToDecimalCoordinates(coordinates) {
        // Regular expression to match the coordinate format 12°23'56" N, 56°78'91" E
        var regex = /^(\d+)°(\d+)'(\d+(?:\.\d+)?)\" ([NS])(?:,| |, )(\d+)°(\d+)'(\d+(?:\.\d+)?)\" ([EW])$/;
        var match = coordinates.match(regex);
        if (match) {
            var latDegrees = parseInt(match[1]);
            var latMinutes = parseInt(match[2]);
            var latSeconds = parseFloat(match[3]);
            var latDirection = match[4];
            var lonDegrees = parseInt(match[5]);
            var lonMinutes = parseInt(match[6]);
            var lonSeconds = parseFloat(match[7]);
            var lonDirection = match[8];

            // Convert to decimal coordinates
            var latDecimal = latDegrees + (latMinutes / 60) + (latSeconds / 3600);
            if (latDirection === 'S') {
                latDecimal = -latDecimal;
            }

            var lonDecimal = lonDegrees + (lonMinutes / 60) + (lonSeconds / 3600);
            if (lonDirection === 'W') {
                lonDecimal = -lonDecimal;
            }

            return [latDecimal, lonDecimal];
        }
        return null;
    }

it does work, but the text input is overwritten with the decimal coordinate. And I admit, setting up a regex that covers all possible user inputs is going to be very demanding. But this allows to input either comma separated or space separated 12°34'56.78" N, 56°78'9.10" E coordinates.

Would you have a better idea? I am not sure if the search api can handle the geo format. Or should I PR this hacky solution?

Danfro avatar Aug 17 '25 21:08 Danfro