Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function initMap() {
- var map = new google.maps.Map(document.getElementById('map'), {
- center: {lat: 50, lng: -1},
- zoom: 3
- });
- var infoWindow = new google.maps.InfoWindow({map: map});
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- var pos = {
- lat: position.coords.latitude,
- lng: position.coords.longitude
- };
- infoWindow.setPosition(pos);
- infoWindow.setContent('Location found: (lat/long): [' + roundToNPlaces(pos.lat,1) + '][' + roundToNPlaces(pos.lng,1) + ']');
- map.setCenter(pos);
- }, function() {
- handleLocationError(true, infoWindow, map.getCenter());
- });
- } else {
- // Browser doesn't support Geolocation
- handleLocationError(false, infoWindow, map.getCenter());
- }
- }
- function handleLocationError(browserHasGeolocation, infoWindow, pos) {
- infoWindow.setPosition(pos);
- infoWindow.setContent(browserHasGeolocation ?
- 'Error: The Geolocation service failed.' :
- 'Error: Your browser doesn\'t support geolocation.');
- }
- // Utility functions
- //----------------------
- // Round (a float) to the required number of decimal places.
- // This is to compensate for js only rounding to whole integers
- // just used for my case - not tested outside that need
- function roundToNPlaces(numberToRound, decimalPlaces) {
- var placesConvenience = Math.pow(10, decimalPlaces);
- return (Math.round(numberToRound * placesConvenience))/placesConvenience;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement