How to get location from address

dunski

Member
Licensed User
Longtime User
Hi

I have a database of addresses of stores which I need to display with Icons for each on a map. I know how do it on a mapview if I had the longitude and latitude's of the stores but I do not have any.
Does anyone know how to get the longitude and latitude from an address?
Or if it can be done without the longitude and latitudes and ony need the addresses.

Thanks in advance.

Dunski:sign0163::sign0085::sign0089:
 

dunski

Member
Licensed User
Longtime User

Hi Mario
Thanks a million for that. I had a quick look at that earlier but couldn't see how to do it so I played around with it a bit more and got it working.

In case anyone else might find this useful here is some code.

Here's how to get the longitude and latitude if all you have is an address.

B4X:
Dim i As Int
   Dim ad As Address      'Declare as address
   Dim myAdd() As Address 'Declare this one as an array address
   Dim gg As Geocoder
   Dim aDD As String
   
   gg.Initialize
   aDD="11, drogheda street, balbriggan, co dublin, ireland"
   myAdd= gg.GetFromLocationName(aDD,5)

   For i = 0 To myAdd.Length-1
      ad=myAdd(i)' assign the array address object element to the address object , 
                'then you get access to all attributes and methods
      Log("latitude =" & ad.Latitude)
      Log("longtude =" & ad.Longitude)
   Next
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You ought to consider geocoding all the database rows once and saving the geocoded latitude and longitude to the database in additional columns.

It makes no sense to geocode many addresses each time you run your application.

Martin.
 
Upvote 0

dunski

Member
Licensed User
Longtime User
Hi Martin.
Thanks for the advice. Your right a seperate program to update all the rows with the 2
new fields is the way forward. Much faster.
Ill do it that way. Thanks,
Dunski
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You'll have to watch that batch geocoding doesn't cause your IP address to get a temporary ban from the web service that the Geocoder class uses.

The Google javascript and HTTP geocoder web services all imposes limits - you cannot repeatedly query the service in too small a period of time and you cannot query the service more than ?? times per days:

https://developers.google.com/maps/articles/geocodestrat#quota-limits

I've had a search but cannot find whether the Android Geocoder has any usage limits - it may or may not.
If you try to geocode many addresses in a loop and it starts to fail i'd suspect that the Android Geocoder does have limits and you've been banned!

If you have all your addresses in a MySQL database (or can import them into a MySQL database) then you may be able to adapt this PHP script to batch geocode them all with a variable delay between each geocode attempt:

PHP:
<?php

set_time_limit(0);

require('db_login.php');
$db=mysql_connect($host, $username, $password) or die('Database connection failed: '.mysql_error());
mysql_select_db($db_name, $db) or die('Database selection failed: '.mysql_error());

$query_delay=1500000;
$query_count=0;

$table_names=array('your_table_name_here');
$length=count($table_names);
for($i=0; $i<$length; $i++){
   $mysql_query="SELECT id, ADDRESS_1, ADDRESS_2, POST_TOWN, COUNTY, POST_CODE from {$table_names[$i]} WHERE lat=0 AND lng=0";
   $mysql_result=mysql_query($mysql_query) or die('Database query failed: '.mysql_error());
   while(list($id, $address1, $address2, $post_town, $count, $postcode)=mysql_fetch_row($mysql_result)){
      $geocode_query='http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='.rawurlencode($address1).'+'.rawurlencode($address2).'+'.rawurlencode($post_town).'+'.rawurlencode($county).'+'.rawurlencode($postcode);
      $geocode_result=file_get_contents($geocode_query);
      $query_count++;
      $geocode_json=json_decode($geocode_result, true);
      /*   uncomment to debug any errors
      switch(json_last_error()){
         case JSON_ERROR_DEPTH:
            echo 'Maximum stack depth exceeded';
            break;
         case JSON_ERROR_CTRL_CHAR:
            echo 'Unexpected control character found';
            break;
         case JSON_ERROR_SYNTAX:
            echo 'Syntax error, malformed JSON';
            break;
         case JSON_ERROR_NONE:
            echo 'No errors';
            break;
      }   */
      /*   echo '<br><pre>';
      print_r($geocode_json);
      echo '</pre><br>';   */
      if($geocode_json['status']=='OK'){
         $lat=$geocode_json['results'][0]['geometry']['location']['lat'];
         $lng=$geocode_json['results'][0]['geometry']['location']['lng'];
      } else {
         //   these values will be used if the geocode fails
         $lat=360;
         $lng=360;
      }
      $update_query="UPDATE {$table_names[$i]} SET lat=$lat, lng=$lng WHERE id=$id LIMIT 1";
      $update_result=mysql_query($update_query) or die('Database update failed: '.mysql_error());
      
       if($geocode_json['status']=='OVER_QUERY_LIMIT'){
         $query_delay+=1500000;
      }
      if($query_count>2500){
         die("Exiting. Query count: $query_count");
      }
      usleep($query_delay);
   }
   echo "Complete. Query count: $query_count";
}
?>

I used that script once to geocode three MySQL tables which contained (very) many addresses.
Even with the delay i found i had to reboot my router a few times so that i got a new IP address and could continue geocoding.

The script will try and geocode then update any row where lat=0 AND lng=0, if a geocode fails it updates lat=360 and lng=360.

Martin.
 
Upvote 0

dunski

Member
Licensed User
Longtime User
Hi Martin,
I geocoded about 1000 addresses without a hitch this morning from my mysql database.
I just did it the way I wrote the code above.
Its obviously a low enough number thankfully. However in time there will be need to geocode many times more than a thousand so I will use your script above for that when the time comes.

Thanks a mill for all your help and cool libraries...
Dunski.
 
Upvote 0
Top