HTML5 Geolocation, with Google Map Integration

Just a simple use of the HTML5 Geolocation API

by Matthew Schenker

HTML

<script src="https://maps.google.com/maps/api/js?sensor=true%22"></script>
<div class="map_container">
  <div class="map_box">
  </div>
</div>

JavaScript

function success(position) {
  var mapcanvas = document.createElement('div');
  mapcanvas.id = 'mapholder';
  mapcanvas.style.height = '800px';
  mapcanvas.style.width = '800px';

  document.querySelector('.map_box').appendChild(mapcanvas);

  var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

  var options = {
    zoom: 15,
    center: coords,
    mapTypeControl: false,
    navigationControlOptions: {
      style: google.maps.NavigationControlStyle.SMALL
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapholder"), options);

  var marker = new google.maps.Marker({
    position: coords,
    map: map,
    title: "You are here!"
  });
}

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success);
} else {
  error('Geo Location is not supported');
}