Use Google-Geocode API to get location string from coordinates

Thanks Bredmich and NCO.
That is a great help.

I did find the list of other location properties so will add them when I have time, they are on the OpenStreetMap wiki pages.
Address Key: https://wiki.openstreetmap.org/wiki/Key:addr
Amenity List: https://wiki.openstreetmap.org/wiki/Key:amenity

I´m not sure if this properties are used for the reverse lookup.
I didn´t had any of them in my results.

Here´s a typical result for my lookups:

{"place_id":"#",
"licence":"Data Š OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type":"way",
"osm_id":"#",
"lat":"51.#",
"lon":"7.#",
"display_name":"#, # #, #, #, # #, #-#, #, Deutschland",
"address":{"house_number":"#",
"road":"# #",
"suburb":"#",
"city":"#",
"county":"#",
"state_district":"# #",
"state":"#-#",
"postcode":"#",
"country":"Deutschland",
"country_code":"de"}
,"boundingbox":["51.#","51.#","7.#","7.#"]}

Maybe i´ll start logging the raw results to have a look at them later.

I had a quick run through my events.log files and found the following:

“junction”
“county”
“hamlet”
“furniture”
“house_number”
“wood”
“fast_food”

There was a complete list here: https://wiki.openstreetmap.org/wiki/Map_Features

In the end I just used the display_name

val String result = sendHttpGetRequest(request)
logInfo(“lookupAddress”, result)
val String dis_name = transform(“JSONPATH”, “$.display_name”, result)
val String address = dis_name
return address

Sometimes the postcode drops off the end of the sitemap but I don’t mind.
Thanks for your help

Sorry, for bringing this topic up again.
But the code always returns the whole JSON response instead of the transformed string.
Seems that
transform("JSONPATH", "$.XXXXX", result)
does not work correctly

Forgot to install the “JSONPath Transformation” extension. Just thought it was enabled by default. Never mind

Perfect it work but only for one iPhone,
I copy the code for an 2nd Device, but for both devices google send my the same location name.
What have to be different in the code for the second device?

I am struggling with the OpenStreetMap reverse address lookup. When the rule is executed I get the following error:

2021-10-04 21:10:52.013 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'icloudChristian-1' failed: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in icloudChristian
javax.script.ScriptException: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in icloudChristian

Any help to solve this would be highly appreciated.

I use this rule (thanks Bredmich):

val Functions$Function1<GenericItem, String> lookupAddress= [ ChristianiPhone_Location |

    // get lat lon from item
    val PointType location = ChristianiPhone_Location.state as PointType
    val String lat = location.latitude.toString
    val String lon = location.longitude.toString

    // build request
    val String endpoint = 'https://nominatim.openstreetmap.org/reverse'
    val String lang     = 'da-DK'
    val String query    = 'lat=' + lat + '&lon=' + lon + '&accept-language=' + lang + '&format=json&addressdetails=1'
    val String request  = endpoint + '?' + query

    // get result
    val String result  = sendHttpGetRequest(request)
    // logInfo("lookupAddress", result)
    var String road    = transform("JSONPATH", "$.address.road", result)
    var String house   = transform("JSONPATH", "$.address.house_number", result)
    var String city    = transform("JSONPATH", "$.address.city", result)
    var String town    = transform("JSONPATH", "$.address.town", result)
    var String country = transform("JSONPATH", "$.address.country", result)
    var String hide    = 'Danmark' // in result language; hide your home country

    // Empty the Strings if they´re not part of the result and the full result was put into the Strings
    if(road.startsWith("{"))
    {
        road = ''
    } 
    if(house.startsWith("{"))
    {
        house = ''
    }
    if(town.startsWith("{"))
    {
        town = ''
    }
    if(city.startsWith("{"))
    {
        city = ''
    }
    if(country.startsWith("{"))
    {
        country = ''
    }

    // Check if city is empty and fill in town
    if(city == '')
    {
        city = town
    }

    // Check if city is still empty and fill Unknown
    if(city == '')
    {
        city = 'Unknown'
    }

    var String address  = road + ' ' + house + ', ' + city

    // Check if the address starts with comma and only use the city
    if(address.startsWith(",") || address.startsWith(" ,"))
    {
        address = '' + city
    }

    if (country != hide) {
        address = address + ', ' + country
    }

    return address
]

rule "ChristianHjemme"


when

    Item ChristianiPhone_Location changed

then

    // Definition von Zuhause und aktueller Standort
    val PointType homeLocation = new PointType(new DecimalType(55.XXXXX), new DecimalType(12.XXXXX))
    val PointType phoneLocation = ChristianiPhone_Location.state as PointType
    val int distance = phoneLocation.distanceFrom(homeLocation).intValue()

    // Remove _Location from the triggering item and add _Address
    val String updateItem = triggeringItem.name.toString.split("_Location").get(0) + '_Address'

    // Fill in the address into the item
    postUpdate(updateItem, lookupAddress.apply(triggeringItem))

    if(distance < 150)
    {
        iPChristian_Home.postUpdate(ON)
        logDebug("ChristianHome", "Christian er hjemme")
    }
    else
    {
        iPChristian_Home.postUpdate(OFF)
        logDebug("ChristianHome", "Christian er ude")
    }

end

Christian