Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Country code/name from coordinates?



  • Hello :)
    Is there a possibility to get country code/name from coordinates( latitude & longitude )?
    I would like to provide some feature on the base of country.
    I would be grateful for any help or link :)


  • Moderators

    @ErnestJ
    the term you are looking for is "reverse geocoding"
    GeoCodeModel provides such functionality for example



  • @raven-worx
    Thank you for help. While trying out example from your link I encountered a network error.

        Plugin {
                id: mapPlugin
                name: "osm"
        }
        GeocodeModel {
            id: geocodeModel
            plugin: mapPlugin
            autoUpdate: false
        }
        MouseArea {
            id: mouseArea
            x: 75
            y: 101
            width: 200
            height: 200
            onClicked: {
                geocodeModel.query = "10.075252, 19.934158"
                geocodeModel.update()
                if(geocodeModel.count > 0)
                    console.log( geocodeModel.get(0).address.country )
                else
                    console.log("no connection")
            }
        }
    

    Always first MouseArea click gives "no connection", but second shows correct result. Could you tell me why is it? Should I somehow connect to osm first?
    Output:

    qml: no connection
    QNetworkReplyHttpImplPrivate::_q_startOperation was called more than once QUrl("https://nominatim.openstreetmap.org/search?q=10.075252, 19.934158&format=json&accept-language=en&polygon_geojson=1&addressdetails=1")
    qml: Chad
    

  • Moderators

    @ErnestJ said in Country code/name from coordinates?:

    Always first MouseArea click gives "no connection", but second shows correct result. Could you tell me why is it?

    of course , because (like any other network request) the result comes asynchronously.
    So rather do something like this:

    GeocodeModel {
        ...
        onCountChanged: {
            if( count > 0 )
                  console.log( geocodeModel.get(0).address.country )
        }
    }
    

Log in to reply