address to latitude and longitude?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

address to latitude and longitude?

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: address to latitude and longitude?

Post by jassing »

Fips
User
User
Posts: 35
Joined: Sun Feb 20, 2022 1:03 pm

Re: address to latitude and longitude?

Post 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.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: address to latitude and longitude?

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: address to latitude and longitude?

Post 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.
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: address to latitude and longitude?

Post by idle »

Pb OpenStreetMap should have that
Post Reply