Getting address from latitude/longitude value using Google API and PhoneGap

Introduction:
In this post we will discover the use of Google API to get the current address and type of address by using the PhoneGap geolocation API and Google API.

Description:
Sometimes we need to get the address of a location from geolocation value. It is very very easy to get the same using Google API and PhoneGap. First we need to get the geolocation value i.e latitude and longitude using PhoneGap geolocation API.

Here are the steps that we need to follow.

1. Include the Phonegap/Cordova JavaScript file and Google API JavaScript file inside your html page.

<script type="text/javascript" src="phonegap/cordova.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"></script>


2. Add the eventlistner to load the function when device gets ready.

	document.addEventListener("deviceready", GetGeoLocation , false);

3. Get the geolocation value using following JavaScript code.

function GetGeolocation()
{
    var options = { timeout: 30000, enableHighAccuracy: true };
    	navigator.geolocation.getCurrentPosition(GetPosition, PositionError, options);
}

function GetPosition(position)
{
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      ReverseGeocode(latitude,longitude);   // Pass the latitude and longitude to get address.
}

function PositionError() {
      navigator.notification.alert('Could not find the current location.');
}

4. Use the following function to get the address and type of address.

function ReverseGeocode(latitude, longitude){
 	var reverseGeocoder = new google.maps.Geocoder();
	var currentPosition = new google.maps.LatLng(latitude, longitude);
	reverseGeocoder.geocode({'latLng': currentPosition}, function(results, status) {

     		if (status == google.maps.GeocoderStatus.OK) {
           			if (results[0]) {
	    			navigator.notification.alert('Address : ' + results[0].formatted_address + ',' + 'Type : ' + results[0].types);
            		}
			else {
	     			navigator.notification.alert('Unable to detect your address.');
           			}
		} else {
			navigator.notification.alert('Unable to detect your address.');
		}
	});
}

5. Finally create a button and call the GetGeolocation function on its click event.

   <button onclick="GetGeolocation()">Get Current Address</button>

Summary:
From the above example we get to know how to get the address for current geolocation value using Google API and PhoneGap.Written By: Sourabha Kumar Sahoo, Software Developer, Mindfire Solutions

Advertisements

7 thoughts on “Getting address from latitude/longitude value using Google API and PhoneGap

  1. Pingback: Getting address from latitude/longitude value using Google API and PhoneGap | souravsahoo

  2. 04-29 15:03:13.310: E/Web Console(1933): Uncaught ReferenceError: google is not defined:115

    i got this error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s