Created
June 26, 2012 12:35
-
-
Save jaymzcd/2995572 to your computer and use it in GitHub Desktop.
geo locate via html5 js, modified from html5 demo site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <meta name="viewport" content="width=620" /> | |
| <style type="text/css"> | |
| body { | |
| width: 800px; | |
| margin: 0 auto; | |
| } | |
| </style> | |
| <h1>HTML5 Geolocation Example</h1> | |
| <p>This works on mobile just as well as desktop browsers</p> | |
| <title>HTML5 Geolocation Example</title> | |
| <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> | |
| <article> | |
| <p>Finding your location: <span id="status">checking...</span></p> | |
| </article> | |
| <script> | |
| function success(position) { | |
| var s = document.querySelector('#status'); | |
| if (s.className == 'success') { | |
| // not sure why we're hitting this twice in FF, I think it's to do with a cached result coming back | |
| return; | |
| } | |
| s.innerHTML = "Success!"; | |
| s.className = 'success'; | |
| var resp = document.createElement('p'); | |
| resp.innerHTML = '<em>Your browser says you are: at <strong>('+position.coords.latitude+', '+position.coords.longitude+')</strong></em>'; | |
| document.querySelector('article').appendChild(resp); | |
| var mapcanvas = document.createElement('div'); | |
| mapcanvas.id = 'mapcanvas'; | |
| mapcanvas.style.height = '400px'; | |
| mapcanvas.style.width = '800px'; | |
| document.querySelector('article').appendChild(mapcanvas); | |
| var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); | |
| var myOptions = { | |
| zoom: 15, | |
| center: latlng, | |
| mapTypeControl: false, | |
| navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, | |
| mapTypeId: google.maps.MapTypeId.ROADMAP | |
| }; | |
| var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions); | |
| var marker = new google.maps.Marker({ | |
| position: latlng, | |
| map: map | |
| }); | |
| } | |
| function error(msg) { | |
| var s = document.querySelector('#status'); | |
| s.innerHTML = typeof msg == 'string' ? msg : "failed"; | |
| s.className = 'fail'; | |
| // console.log(arguments); | |
| } | |
| if (navigator.geolocation) { | |
| navigator.geolocation.getCurrentPosition(success, error); | |
| } else { | |
| error('not supported'); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment