• Category Category: Android
  • Comment Comments: 33
  • View View: 3124

In this article I am going to use html5 geolocation api to find your coordinates and by using these coordinates and Google map, you can find you location on the map.

HTML5 GeoLocation:

One Shot Position request: in this case we will use one shot position request because we only want the coordinates once. Before continue the original topic first I take look HTML5 Geolocation position accuracy, altitude, and permission.

Position and Position Accuracy

Different devices have different degrees of accuracy and it is important that your application be aware of the difference. A cell phone that has a GPS unit inside of it that is switched on is usually accurate within three meters. A cell phone without a GPS unit, with the GPS unit switched off to maximize battery, or at a location where the GPS can’t contact the GPS satellites will have to use cell tower triangulation to estimate the users location and is typically accurate within 3000 meters which is accurate enough to know what neighborhood the user is in but completely useless to tell them what building they are looking at.

If the user is accessing your site from a computer connected to a land-based broadband connection it can usually pinpoint the precise address by consulting a provider database and pinpointing the exact address from the DSL or cable provider.

To get the accuracy of the location information, you can query the accuracy property on the coords object. The accuracy property isn’t exact by any means but it will give your application a good sense as to whether or not you have a nearly precise position or a neighborhood.

Altitude and Altitude Accuracy

Most GPS enabled devices are capable of reporting altitude but non-GPS methods such as cell triangulation cannot report altitude. Altitude is frequently overlooked, it could be used in a variety of innovate ways to enable games, help users track skydiving, mountain climbing, position in a skyscraper and more.

If the device does not support altitude information, the altitudeAccuracy property on the position object will be null.

There is also support for the W3C Geolocation API on mobile devices:

  • Android 2.0+
  • iPhone 3.0+
  • Opera Mobile 10.1+
  • Symbian (S60 3rd & 5th generation)
  • Blackberry OS 6
  • Maemo

Permission, Latency and Error Handling

The HTML 5 specification explicitly requires the user grant permission to any web page requesting geolocation information. This means the request for location might still error even if the code has already checked for browser support if the user declines to provide your application with access.

Geolocation is not instantaneous. It usually takes between 1 and 20 seconds depending on the GPS device being used and whether or not the device already has a lock on the current location.

As a result of both the requirement that the browser ask the user to grant geolocation permission and the GPS time lag, the request for geolocation information is an asynchronous call.

One Shot Position request

Now, take a look on getCurrentPosition() function.

void getCurrentPosition(in PositionCallback successCallbackin optional PositionErrorCallback errorCallback,

in optional PositionOptions options);

First, this is a function that is available on the navigator.geolocation object, so you will need to have already retrieved this object in your script. As noted previously, make sure that you have a good fallback handler if your browser does not support HTML5 Geolocation.

The function takes one required parameter, and two optional ones.

  • The successCallback function parameter tells the browser which function you want called when the location data is made available.
  • The errorCallback function parameter tells the browser which function you want called when there is some error or exception has occurred.
  • Finally, an options object can be provided to the HTML5 Geolocation service to fine-tune the way it gathers data. This is an optional parameter.

Now, I write a JavaScript function GetGeolocation() and called on the button click event.

function GetGeolocation() {

navigator.geolocation.getCurrentPosition(GetCoords, GetError);

}

navigator.geolocation  object is the core of HTML5 geolocation api. Now take a look on GetCoords() function.

function GetCoords(position){

  document.getElementById(“txtLatitude”).value = position.coords.latitude;

  document.getElementById(“txtLongitude”).value = position.coords.longitude;

  document.getElementById(“txtaccuracy”).value = position.coords.accuracy;

  FindmeOnMap(position.coords.latitude, position.coords.longitude);

}

GetCoords() function has an argument of position.The position will contain coordinates—as the attribute coords—and a timestamp for when the location data was gathered. While you may or may not need the timestamp, the coords attribute contains the crucial values for the location.

The coordinates always have multiple attributes on them, but it is up to the browser and the

hardware of the user’s device whether they will have meaningful values. The following are the first three attributes:

  • latitude
  • longitude
  • accuracy

These attributes are guaranteed to have values and are fairly self-explanatory. latitude and longitude will contain the HTML5 Geolocation service’s best determined value of the user’s location specified in decimal degrees. accuracy will contain a value in meters which specifies how close the latitude and longitude values are to the actual location, with a 95% confidence level. Due to the nature of HTML5 Geolocation implementations, approximation will be common and coarse. Make sure to check the accuracy of the returned values before you present them with any certainty. Recommending a user to visit a “nearby” shoe store that is actually hours away could have unintended consequences.

The other attributes of the coordinates are not guaranteed to be supported, but they will return a null value if they are not:

  • altitude—the height of the user’s location, in meters
  • altitudeAccuracy—once again in meters, or null if no altitude is provided
  • heading—direction of travel, in degrees relative to true north
  • speed—ground speed in meters per second

Unless you are sure that your users have devices with access to such information, it is recommended that you not rely on them as critical to your application. While global positioning devices are likely to provide this level of detail, network triangulation will not.

 Google Map

Now, we have the coordinates and will use these to display your location/position on google map. First you have to include the google map JavaScript library as.

<script type=“text/javascript” src=“http://maps.googleapis.com/maps/api/js?sensor=true”> </script>

Also, create an html div element where you want to show your map with some width and height.

<div id=“map” style=“border:1px solid #0a057f; width:60%; height:500px; margin:5px;border-radius:10px;” ></div>

Now, take a look on FindmeOnMap() function.

function FindmeOnMap(lat, long) {

var latlng = new google.maps.LatLng(lat, long);

var myOptions = {

zoom: 12,

center: latlng,

mapTypeId: google.maps.MapTypeId.SATELLITE

};

var map = new google.maps.Map(document.getElementById(“map”), myOptions);

}

To learn more about google map type and how to use them see Google maps.

By using the google map api, create an object of latlong by providing the latitude, longitude values in it constructor, set the options and finally create an object of google map by passing the ‘div’ element and the options.

So, that’s it. This is one of the ways to find the positions on google map by using html5 geolocation. Here is the small code to test this reading. Just copy and paste on your html page and test it.

<!DOCTYPE><html>

<head>

<title></title>

<script type=“text/javascript” src=“http://maps.googleapis.com/maps/api/js?sensor=true”> </script>

<script type=“text/javascript”>

function GetGeolocation() {

navigator.geolocation.getCurrentPosition(GetCoords, GetError);

}

function GetError(error)

{

switch(error.code)

{

case error.PERMISSION_DENIED:

alert(‘user did not share geolocation data’);

break;

case error.POSITION_UNAVAILABLE:

alert(‘could not detect current position’);

break;

case error.TIMEOUT:

alert(‘retrieving position timed out’);

break;

default:

alert(‘unknown error’);

break;

}

}

function GetCoords(position) {

document.getElementById(“txtLatitude”).value = position.coords.latitude;

document.getElementById(“txtLongitude”).value = position.coords.longitude;

document.getElementById(“txtaccuracy”).value = position.coords.accuracy + ” meters”;

FindmeOnMap(position.coords.latitude, position.coords.longitude);

}

function FindmeOnMap(lat, long) {

var latlng = new google.maps.LatLng(lat, long);

var myOptions = {

zoom: 20,

center: latlng,

mapTypeId: google.maps.MapTypeId.SATELLITE

};

var map = new google.maps.Map(document.getElementById(“map”), myOptions);

}

</script>

</head>

<body>

Latitude: <input type=“text” id=“txtLatitude”/>

Longitude: <input type=“text” id=“txtLongitude”/>

Accuracy: <input type=“text” id=“txtaccuracy”/>

<input type=“button” value=“Find My Location” onclick=“GetGeolocation()”/>

<div id=“map” style=”border:1px solid #0a057f; width:60%; height:500px; margin:5px;border-radius:10px;”>

</div>

</body>

</html>

admin Article's Source: http://codeheaven.org/find-position-on-google-map-using-html5-geolocation-for-android-and-iphone.html
Author:

  • authorBy:
  • writerPosted On: November 5, 2011
  • livePublished articles: 36

Comments (33)

  • Robert
    Robert Say (March 7, 2012 at 10:57 am)

    Hi! I want to know if can obtain my phone location using web application without installing any application on my phone. Is possible this thing?

    Thanks

  • Tas laptop
    Tas laptop Say (January 30, 2012 at 5:17 am)

    My spouse and I absolutely love your blog and find a lot of your post’s to be just what I’m looking for. can you offer guest writers to write content to suit your needs? I wouldn’t mind composing a post or elaborating on a few of the subjects you write regarding here. Again, awesome weblog!

  • fruit mocking party
    fruit mocking party Say (December 24, 2011 at 11:15 am)

    Woah! I’m really enjoying the template/theme of this site. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and appearance. I must say you’ve done a fantastic job with this. In addition, the blog loads super quick for me on Firefox. Excellent Blog!

  • blinder
    blinder Say (December 24, 2011 at 6:27 am)

    Just ran across your web site, I have to say i was very satisfied with the knowledge you discussed. Often It’s challenging to find information thats truly worth reading. I will be checking back from time to time to read through much more. Cheers for the share!

  • Raquel Kantor
    Raquel Kantor Say (December 23, 2011 at 6:04 pm)

    I like this post, enjoyed this one regards for posting .

  • graco open top swing
    graco open top swing Say (December 22, 2011 at 10:57 pm)

    Just a smiling visitant here to share the love (:, btw outstanding style .

  • bet
    bet Say (December 7, 2011 at 7:09 pm)

    Highly interesting post. This is a very entertaining post. I look forward to see even more in the near future.

  • arcopedico
    arcopedico Say (December 7, 2011 at 5:59 pm)

    Spot on with this write-up, I truly assume this web site wants much more consideration. I’ll in all probability be once more to read way more, thanks for that info.

  • ugg boots pink
    ugg boots pink Say (December 7, 2011 at 10:13 am)

    Your website is not only informative but very artistic too. You will find often very several people who may write not easy articles which artistically. Carry on the superb writing !!

  • Siobhan Sarwar
    Siobhan Sarwar Say (December 6, 2011 at 1:07 pm)

    Hello There. I found your blog using msn. This is an extremely well written article. I’ll make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely comeback.

  • certainly
    certainly Say (December 6, 2011 at 12:25 am)

    I quite empathize with your outlook on this matter. Thanks so much for writing such a nice article. Have you written similar articles like this one?

  • Fiverr Clone
    Fiverr Clone Say (December 4, 2011 at 2:21 pm)

    Do you mind if I quote your writing on my Wiki Site? I think your writing would suit my readers perfectly. Well ya, thanks for posting this article.

  • Matskin
    Matskin Say (December 2, 2011 at 11:38 am)

    Appreciate it for the superb writeup. Anyhow, exactly how could we talk?

    • Lucky
      Lucky Say (December 7, 2011 at 2:57 pm)

      That’s really thninkig out of the box. Thanks!

    • Gertie
      Gertie Say (December 9, 2011 at 1:40 am)

      Great atrilce, thank you again for writing.

  • Xexilia
    Xexilia Say (November 28, 2011 at 2:36 am)

    It’s impaetrvie that more people make this exact point.

    • Blaze
      Blaze Say (December 7, 2011 at 8:16 am)

      It’s great to read something that’s both enjoyable and provdies pragmatisdc solutions.

  • Kevrel
    Kevrel Say (November 28, 2011 at 1:10 am)

    Way to go on this essay, helepd a ton.

  • kids bailey button
    kids bailey button Say (November 16, 2011 at 12:19 pm)

    Pleased to be visiting your site once again, it’s been days for me personally. Well, this is actually the article which Ive been waited for so long. Many thanks,

    • admin
      admin Say (November 16, 2011 at 7:01 pm)

      thanks!

      • Lynda
        Lynda Say (November 28, 2011 at 3:29 am)

        Very valid, pithy, succicnt, and on point. WD.

        • Loradae
          Loradae Say (December 7, 2011 at 2:22 pm)

          Shoot, so that’s that one spuposes.

        • Issy
          Issy Say (December 9, 2011 at 2:02 am)

          Super ecxtied to see more of this kind of stuff online.

  • 大陸抓姦
    大陸抓姦 Say (November 16, 2011 at 2:19 am)

    Good idea. Can be considered a learned thing, ok!

    • admin
      admin Say (November 16, 2011 at 6:59 pm)

      thanks

    • Torn
      Torn Say (November 28, 2011 at 2:05 am)

      That’s way more cleevr than I was expecting. Thanks!

  • imran saeed
    imran saeed Say (November 15, 2011 at 11:43 am)

    nice API …… for html5

  • magento design guide
    magento design guide Say (November 12, 2011 at 7:48 pm)

    I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back later on. Many thanks

  • Ileen Mancias
    Ileen Mancias Say (November 11, 2011 at 10:17 am)

    Here is a Great Weblog You will probably find Fascinating that individuals Encourage You.

  • jigger
    jigger Say (November 9, 2011 at 12:37 pm)

    This article is one of the best which i have read in this platform. I saw many comments below this text. They’re very glad to see the article when i.

  • Chere Bestwick
    Chere Bestwick Say (November 9, 2011 at 7:18 am)

    Nice This Information…
    Keep It Up….

    • Bette
      Bette Say (November 28, 2011 at 3:54 am)

      I don’t know who you wrote this for but you helped a borhter out.

      • Priest
        Priest Say (December 9, 2011 at 5:19 am)

        THX that’s a great aswner!

Post Comment


4 + = five

 
May 2013
M T W T F S S
« Oct    
 12345
6789101112
13141516171819
20212223242526
2728293031  
Technologies