October 01, 2014, 07:44:05 PM

Author Topic: Can you tell me what Geolocation APIs in HTML for Android?  (Read 207 times)

geolocation

  • Guest
Can you tell me what Geolocation APIs in HTML for Android?
« on: October 11, 2013, 10:46:08 PM »
How they are used? Why they are used?

I am trying to learn about the Geolocation APIs which are used as Web based HTML API in  Android, Can you tell how they are used? Why they are used?

I am kinda very new to this so can you please give me some proper details about it?



Offline infomaniac

  • Administrator
  • Full Member
  • *****
  • Thank You
  • -Given:
  • -Receive:
  • Posts: 163
  • Karma: +0/-0
    • View Profile
    • Poke This Member
Re: Can you tell me what Geolocation APIs in HTML for Android?
« Reply #1 on: October 11, 2013, 10:56:04 PM »
The reason behind using HTML is it has a simple solution for geolocation and in just few lines of code with it you can easily get the current location and the updates of the location. After these things it gets a little complicated though.

Basic API for getting a location is rather simple which only get the latitude and longitude. HTML is more like a vendor-neutral platform which in sense we are more like free to use any of the geocoding and mapping service which we wish. Now to understand it better I ll give a small example of getting the current location using the HTML standard API which uses the Google geocoding API to obtain a proper address for the current location which can be later printed on the screen.

The code to follow for getting the current location is as follows,

navigator.geolocation.getCurrentPostion(get_current_position);

Now the getCurrentPosition as a function to itself so what does the get_current_position function will do? the code snippet is as follows,

function get_current_position(postion){
var latitude = position.coords.latitude; //gives you the current latitude of your location.
var longtitude = position.coords.longitude; //gives you the current longitude of your location.
console.log(“The Latitude is:” + latitude);
console.log(“The Longitude is:” + longitude);
}

Now from the above code we can see that we got the right latitude and longitude of the place so the next step to follow is to get the address of the particular location which can be obtained by embedding Google geocoding API. To do this we have to include a script tag which will add up the Google Maps JavaScript library. The script code is as follows,

<script type=”text/javascript” src=”https://maps.googleapis.com/maps/js?sensor=false”> </script>

From the above script tag we can include the Google Maps JavaScript library now comes the real code which gives you the output as the proper formatted address which as follows,

var geoCoder=new google.maps.Geocoder();
var latitudeLng= new google.maps.LatLng(latitude, longitude);
geoCoder.geocode({‘latitudeLng’:latitudeLng}, function(results,status) {
console.log(results[1].formatted_address);
});


From the above code, we can see how the Google geocoder object is created and then adding the latitude and longitude to get the current location in the maps and the geocode function is invoked with first param of the latitude and longitude as the input and the second param which invokes a function by itself which prints simply the formatted address of the given latitude and longitude respectively.

If you enclose the whole snippet code with <HTML></HTML> makes the app work with showing the location. In this way you can carry out working with the HTML APIs in Android which makes things more simpler and easier too.
« Last Edit: November 18, 2013, 05:11:08 PM by infomaniac »

 
Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
Copyrights © InfoManiac

Related Posts Plugin for WordPress, Blogger...