B4A Library Geocoder library

Geocoder library

The Geocoder library enables you to geocode coordinates and reverse geocode addresses.

The library adds two new Objects to the IDE:

Version: 2.1
  • Address
    Methods:
    • HasLatitude As Boolean
      Returns True if a latitude has been assigned to this Address, False otherwise.
    • HasLongitude As Boolean
      Returns True if a longitude has been assigned to this Address, False otherwise.
    • IsInitialized As Boolean
    Properties:
    • AddressLines As List [read only]
      Returns a List of Strings which are the address lines of the Address.
    • AdminArea As String [read only]
      Returns the administrative area name of the Address.
      For example, "CA", or Null if it is unknown.
    • CountryCode As String [read only]
      Returns the country code of the Address.
      For example "US", or Null if it is unknown.
    • CountryName As String [read only]
      Returns the localized country name of the Address.
      For example "Iceland", or Null if it is unknown.
    • FeatureName As String [read only]
      Returns the feature name of the Address.
      For example, "Golden Gate Bridge", or Null if it is unknown.
    • Latitude As Double [read only]
      Returns the latitude of the Address if known.
    • Locality As String [read only]
      Returns the locality of the Address.
      For example "Mountain View", or Null if it is unknown.
    • Longitude As Double [read only]
      Returns the longitude of the Address if known.
    • Phone As String [read only]
      Returns the phone number of the Address if known, or Null if it is unknown.
    • PostalCode As String [read only]
      Returns the postal code of the Address.
      For example "94110", or Null if it is unknown.
    • Premises As String [read only]
      Returns the premises of the Address, or Null if it is unknown.
    • SubAdminArea As String [read only]
      Returns the sub-administrative area name of the Address.
      For example, "Santa Clara County", or Null if it is unknown.
    • SubLocality As String [read only]
      Returns the sub-locality of the Address, or Null if it is unknown.
    • SubThoroughfare As String [read only]
      Returns the sub-thoroughfare name of the Address, or Null if it is unknown.
    • Thoroughfare As String [read only]
      Returns the thoroughfare name of the Address.
      For example, "1600 Ampitheater Parkway", or Null if it is unknown.
    • Url As String [read only]
      Returns the public URL for the Address, or Null if it is unknown.
  • Geocoder
    Events:
    • GeocodeDone (Addresses() As Address, Tag As Object)
    Methods:
    • GetFromLocation (Latitude As Double, Longitude As Double, MaxResults As Int, Tag As Object)
      Gets an Array of Address objects that describe the area immediately surrounding the given Latitude and Longitude.
      The Array of Address objects is passed back to B4A in the GeocodeDone event along with the Tag Object.
      Tag is not used by this method, it is simply a means by which you can associate an identifying Object with this task.
    • GetFromLocationName (LocationName As String, MaxResults As Int, Tag As Object)
      Gets an Array of Address objects that describe the named location.
      LocationName may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", etc...
      The Array of Address objects is passed back to B4A in the GeocodeDone event along with the Tag Object.
      Tag is not used by this method, it is simply a means by which you can associate an identifying Object with this task.
    • GetFromLocationName2 (LocationName As String, MaxResults As Int, LowerLeftLatitude As Double, LowerLeftLongitude As Double, UpperRightLatitude As Double, UpperRightLongitude As Double, Tag As Object)
      Returns an array of Addresses that are known to describe the named location within the geographical bounds.
      LocationName may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", etc..
      The Array of Address objects is passed back to B4A in the GeocodeDone event along with the Tag Object.
      Tag is not used by this method, it is simply a means by which you can associate an identifying Object with this task.
    • Initialize (EventName As String)
      Initialize the Geocoder object with the device's system Locale.
    • Initialize2 (EventName As String, Language As String)
      Initialize the Geocoder object with a Locale based on Language.
    • Initialize3 (EventName As String, Language As String, Country As String)
      Initialize the Geocoder object with a Locale based on Language and Country.
    • IsInitialized As Boolean
    • IsPresent As Boolean
      Returns true if the Geocoder methods GetFromLocation and GetFromLocationName etc are implemented.
      Lack of network connectivity may still cause these methods to return null or empty lists.

If any of the three Geocoder methods Get??? fail then the GeocodeDone event will simply be passed an array of zero Address objects.
Any exceptions will be caught and sent to the log.

Library files and demo code attached.

Martin.
 

Attachments

  • Geocoder_v2_10.zip
    21.6 KB · Views: 2,019
Last edited:

warwound

Expert
Licensed User
Longtime User
Geocoder updated to version 2.01

This is a bug fix.
It fixes a bug where events were not correctly raised if you used more than one instance of the Geocoder object.

Version 2.01 is attached to the first post in this thread.

Martin.
 

PhilipBrown

Active Member
Licensed User
Longtime User
Sub _geocodedone was not found

Hi Martin,

Thanks for your fantastic work on OSMDroid. I'm planning to use it in a new app.

I'd like to use Geocoder too but am having problems. I downloaded the library and tried to run GeocoderDemo. It did not run as it seemed to be written for an older version of GetFromLocation with only 3 arguments and without an event. I modded the code but now I get the error:

java.lang.Exception: Sub _geocodedone was not found.

I attach my effort.
 

Attachments

  • GeocoderDemo.zip
    9.1 KB · Views: 317

warwound

Expert
Licensed User
Longtime User
Look at the exception logged:

java.lang.Exception: Sub _geocodedone was not found.

The Geocoder is being initialized with an empty string as the event name, so the Sub being called is _geocodedone and NOT Geocoder1_GeocodeDone.

Update your code so that Geocoder is initialized with the EventName that you need:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Geocoder1.Initialize("Geocoder1")
   End If
   
   Activity.AddMenuItem("GetFromLocationName", "GetFromLocationName")
   Activity.LoadLayout("GetFromLocation")
   
   ResultsList.TwoLinesLayout.ItemHeight=ResultsList.TwoLinesLayout.ItemHeight*2
   ResultsList.TwoLinesLayout.Label.Height=-2
   ResultsList.TwoLinesLayout.SecondLabel.Height=-2
   
End Sub

Martin.

[edit]I've now fixed the demo code in the attachment in the first post[/edit]
 
Last edited:

benji

Active Member
Licensed User
Longtime User
I want use ReverseGeoCoding, and it's works excellent. But i need use this offline, can i do this? how?
 

corwin42

Expert
Licensed User
Longtime User
I want use ReverseGeoCoding, and it's works excellent. But i need use this offline, can i do this? how?
No, this is not possible.

Ok, it would be possible with a large locally stored database but I don't know if there are out of the box solutions for this. At least there is no free solution.
 

warwound

Expert
Licensed User
Longtime User
There is a relatively new project GraphHopper that is trying (or has succeeded) in using open street map data for offline routing.
Not sure if it also provides offline geocoding and reverse geocoding.

Martin.
 

javiers

Active Member
Licensed User
Longtime User
On this page: http://wiki.openstreetmap.org/wiki/Query-to-map
shows a query that allows a list of OSM objects defined by a rectangle.

"Query-to-map is a script to show the results of an database query in a map. The used database is the same like used for Mapnik. The script is designed to work in real-time, so it Should be used with queries small relativamente returning only results. "

Html should be extracted from the object information.
 

padvou

Active Member
Licensed User
Longtime User
Geocoder library


If either of the two Geocoder methods Get??? fail then the GeocodeDone event will simply be passed an array of zero Address objects.
Any exceptions will be caught and sent to the log.

Martin.

So, what can I do if both methods return empty?
 

warwound

Expert
Licensed User
Longtime User
Do you see anything reported in the log - uncheck the 'filter' checkbox.

Does everything still fail even after rebooting your device?

Martin.
 

warwound

Expert
Licensed User
Longtime User
Read this thread all the way through and you'll see it's not uncommon for the Geocoder to fail to return a result.
Sometimes a device reboot fixes the problem, sometimes the problem just fixes itself.
And presumably other times it just fails to work whatever you do - but i've never experienced this myself.
 
Last edited:

George Anifantakis

Member
Licensed User
Longtime User
Hi I initilize using Geocoder1.Initialize2("Geocoder1" , "en") but i got the results in Greek instead English. Should i do somethnig diffrent?
 
Top