Here is really simple and easy tutorial to get a location from an address.

This function proves to be useful in the case you are only the address of a business (restaurants, clubs…) what you want to display on a map for instance.

The 2nd case is when you want to know the distance (Km or miles) between 2 points without the location.

From that you can also determine the distance in minute (by walk for example) between 2 points.

With all this information you can pretty much make a Yelp-like app. (Pretty much..! )

 

Requirement :

To use this source code you will be to have a Google Api Key and activate Google Geocoding Api.

 

Stop talking, let’s see how it looks ..!

 

The source code

 

Before taping any lines of java code, add these 2 dependencies in your build.gradle file in order to be able to use google map features.

 

   compile 'com.google.maps:google-maps-services:0.1.3'
   compile 'com.google.android.gms:play-services:6.5.87'

 

Screen Shot 2015-03-24 at 14.09.39

And now here is the wolf!

 

Screen Shot 2015-03-25 at 17.32.49

 

Once google map included in the build.gradle file and the project is build, you will be able to include google.map package in your package and use it.

Google proposes an API to get the coordinate (latitude and longitude) according to an address.

The format of the address should be: “20 otter close, E152PZ, UK, London” for instance.

It is not a requirement, but more information you give, more accurate will be the results.

From that you can create a LatLng object, with the information getting from the api.

 

 

Really straightforward, isn’t it?

 

 

If you want to go further in geolocation, I invite to have a look the Bonus part, what I find really exciting and useful !

 

 

Source code available here on Github.

 

 

 

BONUS

 

The GeolocationHelper class was I created also contains 3 useful methods (What I consider useful at least for my usage).

Distance

 

The first one calculates the distance between 2 coordinates, and gives you the distance in meter.

 

 

 

public static double distance(double lat1, double lon1, double lat2, double lon2) {
       double theta = lon1 - lon2;
       double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
       dist = Math.acos(dist);
       dist = rad2deg(dist);
       dist = dist * 60 * 1.1515;
       return (dist);
   }

 

 

 

Useful in the case that you know where your businesses (restaurants, café, etc..) and you want how far it is from the user for instance.

 

 

 

DistanceByWalkMiles

 

This function gives you the distance by walk for a user, according to an average speed of 5.5km/h.

The distance given in parameter is in miles.

Also useful in the case that you want to give more information to the user.

 

 

 

public static double distanceByWalkMiles(double miles)
   {
       return miles * 15;
   }

 

distanceByWalkMeter

 

Same idea, but with a distance in meter in parameter of the function.

 

 

public static double distanceByWalkMeter(int meter)
   {
       return (60 * (double)meter) / 5500; // conv to min
   }

 

 

 

 

 

You got it ? Perfect, I hope it will be as  useful as it has been for me !

 

 

If you have any suggestions, feel free to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>