Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

Google Maps and Geolocation

You can use the new google.loader.ClientLocation object to get the user's latitude and longitude and then display the user's location on an embedded map.

<!DOCTYPE HTML>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Google Maps and Geolocation</title>
  <script type="application/javascript" src="http://www.google.com/jsapi?key=YOURKEYHERE"></script>
  <script type="application/javascript">
   google.load("maps", "2");
   function initialize() {
    var map = new google.maps.Map2(document.getElementById("map"));
    if (google.loader.ClientLocation && google.loader.ClientLocation.latitude && google.loader.ClientLocation.longitude) {
     document.getElementById("latitude").innerHTML = google.loader.ClientLocation.latitude;
     document.getElementById("longitude").innerHTML = google.loader.ClientLocation.longitude;
     map.setCenter(new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 10);
    } else {
     map.setCenter(new google.maps.LatLng(0, 0), 0);
    }
   }
   google.setOnLoadCallback(initialize);
  </script>
 </head>
 <body>
  <h1>Google Maps and Geolocation</h1>
  <div id="map" style="width:500px;height:500px"></div>
  <p>Latitude: <span id="latitude">Unknown</span></p>
  <p>Longitude: <span id="longitude">Unknown</span></p>
 </body>
</html>

This uses the user's source IP address; this didn't work directly from ISDSL but did work when I SSH piped through a server overseas.

I have this up on a temporary URI.

0 Comments

Post a Comment

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.