Sunday, November 18, 2012

Find geolocation in mobile browser (android, iphone, blackberry)

You can find the latitude and longitude using jquery mobile js library.

Using following code you can get the latitude and longitude.

Add following script in your document

<script type=”text/javascript” src=”http://code.jquery.com/jquery-1.4.3.min.js”></script>;
<script type=”text/javascript” src=”http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js”></script>;
Then use the following code for

$(document).ready(function() {if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(success, error);} else {error('Geolocation not supported');}});function success(position) {var latt = position.coords.latitudevar long = position.coords.longitude}

I created full example with fetching the city, state, country and region data.

Here is example.

<!DOCTYPE html><html><head><title>My Location</title><link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css"; /><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>;<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script>;<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>;<script type="text/javascript">var geocoder;$(document).ready(function() {geocoder = new google.maps.Geocoder();if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(success, error);} else {error('Geolocation not supported');}});function success(position) {var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);geocoder.geocode({'latLng': latlng}, function(results, status) {for (var ik = 0; ik < results[0].address_components.length; ik++) {var addr = results[0].address_components[ik];var getCountry;var getlocality;var getpostal_code;var getState;var getstreet_address;var getcity;var getadministrative_area_level_2;var getadministrative_area_level_3;if (addr.types[0] == 'country')getCountry = addr.long_name;if (addr.types[0] == 'locality')getlocality = addr.long_name; //Cityif (addr.types[0] == 'postal_code')getpostal_code = addr.long_name;if (addr.types[0] == 'administrative_area_level_1')getState = addr.short_name; //Stateif (addr.types[0] == 'street_address')getstreet_address = addr.long_name;if (addr.types[0] == 'sublocality')getcity = addr.long_name; //under City - city sublevelif (addr.types[0] == 'administrative_area_level_2')getadministrative_area_level_2 = addr.long_name; //city levelif (addr.types[0] == 'administrative_area_level_3')getadministrative_area_level_3 = addr.long_name; //sub city level}//alert(getlocality);});var myOptions = {zoom: 15,center: latlng,mapTypeControl: false,navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},mapTypeId: google.maps.MapTypeId.ROADMAP};var mapcanvas = $('#mapcanvas');var map = new google.maps.Map(mapcanvas[0], myOptions);var marker = new google.maps.Marker({position: latlng,map: map,title:"I am here!"});}function error(msg) {var errMsg = typeof msg == 'string' ? msg : "Geolocation failed";$('#msg').html(errMsg);}</script></head><body><div data-role="page"><div data-role="header"><h1>My Location</h1></div><div data-role="content"><input id="findme" type="button" value="Find Me" /><div id="msg"></div><div id="mapcanvas" style="height: 400px; width: 560px;"></div></div></div></body></html>

Incoming search terms:


Source : digcms[dot]com

0 comments:

Post a Comment