Pages

How to get user location in javascript (YUI)

Hey Guys ,
Here is a simple code to get User's Location (lat and lan) in javascript using YUI.

Why Not to use HTML5?
Html5 is not yet supported by many browsers and have no fall back. YUI implementation falls back up to ip based location.

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Location using YUI (with fallback)</title>
<script src="http://yui.yahooapis.com/3.4.0pr3/build/yui/yui-min.js"></script>
<script>
YUI({gallery: 'gallery-2011.05.12-13-26'}).use('gallery-geo', function(Y) {
Y.Geo.getCurrentPosition(function(response){
if (response.success){
console.log(response.coords.latitude);
console.log(response.coords.longitude);
var e=document.getElementById('geolocation');
e.innerHTML="Your Location :"+response.coords.latitude+","+response.coords.longitude;
}
});
});
</script>
<body>
<div id="geolocation" >
</div>
</body>
</head>
</html>

Demo


Note: If you host the file locally, e.g. file:// , it might not work due to security issues. 

No comments:

Post a Comment

Your comment will inspire me, Please leave your comment