Page 1 of 1

address to latitude and longitude?

Posted: Mon Jun 12, 2023 9:22 am
by Fangbeast
Does anyone know how to lookup/convert a given address to latitude and longitude?

I've added an amateur log record to my current project and wanted to make it a bit easier to add information. Got everything else sorted on that form but this last bit.

Re: address to latitude and longitude?

Posted: Mon Jun 12, 2023 2:04 pm
by jassing

Re: address to latitude and longitude?

Posted: Mon Jun 12, 2023 2:44 pm
by Fips
Hi,

I usually use something like this:

Code: Select all

Structure Address
  street.s
  zipcode.s
  city.s
  Country.s
EndStructure

Structure Coordinates
  lat.s
  lon.s
EndStructure



Procedure.i GetCoordinatesFromAddress(*Address.Address, *Coordinates.Coordinates)
  Protected.i HttpRequest
  Protected.s request
  Protected.i json_nr
  Protected.s request_answer
  
  Protected NewList HttpsRueckgabe.Coordinates()
  
  request = "http://nominatim.openstreetmap.org/search/" + URLEncoder(*Address\street + "," + *Address\zipcode + "," + *Address\city + "," + *Address\Country) +  "?format=json&polygon=1&addressdetails=1"  
  
  HttpRequest = HTTPRequest(#PB_HTTP_Get, request,"",#PB_HTTP_NoSSLCheck)
  If HttpRequest
    
    request_answer = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
    
    FinishHTTP(HTTPRequest)
    
    json_nr = ParseJSON(#PB_Any, request_answer)
    If json_nr
      ExtractJSONList(JSONValue(json_nr), HttpsRueckgabe())
      
      If ListSize(HttpsRueckgabe()) >= 1
        ForEach  HttpsRueckgabe()
          *Coordinates\lat = HttpsRueckgabe()\lat
          *Coordinates\lon = HttpsRueckgabe()\lon
          Break
        Next
        
        FreeJSON(json_nr)
        ProcedureReturn  #True
      Else
        ProcedureReturn #False
      EndIf
    Else
      ProcedureReturn #False
    EndIf
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure


Define MyAddress.Address
Define MyCoords.Coordinates


With MyAddress
  \Country = "Germany"
  \city = "Rust"
  \zipcode = "77977"
  \street = "Europa-Park-Straße 2"
EndWith

If GetCoordinatesFromAddress(@MyAddress, @MyCoords)
  With MyCoords
    Debug "Lat: " + \lat
    Debug "Long: " + \lon
  EndWith
Else
  Debug "Address not found."
  EndIf
  

Originally taken and modified for my use from there (I believe):
viewtopic.php?t=66320

I'm not sure if the structure of addresses is the same in every country (like zipcodes and street names) but it works for my country. Maybe it helps.

Re: address to latitude and longitude?

Posted: Mon Jun 12, 2023 9:58 pm
by Fangbeast
Thanks Jassing and Fips. Will put this into my bucket of "Must use and bash into my code somehow" s it is needed. Working like a demon most days to finish this damned project and this part is needed.

Re: address to latitude and longitude?

Posted: Mon Jun 12, 2023 11:27 pm
by jassing
Fangbeast wrote: Mon Jun 12, 2023 9:58 pm Thanks Jassing and Fips. Will put this into my bucket of "Must use and bash into my code somehow" s it is needed. Working like a demon most days to finish this damned project and this part is needed.
Hope it helps -- FWIW -- I live mildly rurally ( about 3 miles outside of a small city in Washignton state, USA; both my suggestion & @fips code failed to locate my lat/lon - despite the USPS being able to ...

So if you're program is Austrailian-centric - i would look to the government's postal service to see if they provide something. In the USA, the postal service has a web-service that does match my address to an approximate lat/lon.

Re: address to latitude and longitude?

Posted: Tue Jun 13, 2023 6:42 am
by idle
Pb OpenStreetMap should have that