daily pastebin goal
76%
SHARE
TWEET

GeoLoc01 js

Dennisaa Sep 1st, 2015 (edited) 27 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function initMap() {
  2.   var map = new google.maps.Map(document.getElementById('map'), {
  3.     center: {lat: 50, lng: -1},
  4.     zoom: 3
  5.   });
  6.   var infoWindow = new google.maps.InfoWindow({map: map});
  7.  
  8.   if (navigator.geolocation) {
  9.     navigator.geolocation.getCurrentPosition(function(position) {
  10.       var pos = {
  11.         lat: position.coords.latitude,
  12.         lng: position.coords.longitude
  13.       };
  14.  
  15.       infoWindow.setPosition(pos);
  16.       infoWindow.setContent('Location found: (lat/long): [' + roundToNPlaces(pos.lat,1) + '][' + roundToNPlaces(pos.lng,1) + ']');
  17.       map.setCenter(pos);
  18.     }, function() {
  19.       handleLocationError(true, infoWindow, map.getCenter());
  20.     });
  21.   } else {
  22.     // Browser doesn't support Geolocation
  23.     handleLocationError(false, infoWindow, map.getCenter());
  24.   }
  25. }
  26.  
  27. function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  28.   infoWindow.setPosition(pos);
  29.   infoWindow.setContent(browserHasGeolocation ?
  30.                         'Error: The Geolocation service failed.' :
  31.                         'Error: Your browser doesn\'t support geolocation.');
  32. }
  33.  
  34. // Utility functions
  35. //----------------------
  36. // Round (a float) to the required number of decimal places.
  37.  // This is to compensate for js only rounding to whole integers
  38.  // just used for my case - not tested outside that need
  39.  function roundToNPlaces(numberToRound, decimalPlaces) {
  40.    var placesConvenience = Math.pow(10, decimalPlaces);
  41.    return (Math.round(numberToRound * placesConvenience))/placesConvenience;
  42.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top