Skip to main content
Known Participant
November 8, 2017
Question

It is possible to get my current location Google maps with JavaScript?

  • November 8, 2017
  • 1 reply
  • 557 views

I found this code on web... But I don't know if this works...

var map = new google.maps.Map(document.getElementById('map'), {

    center: {lat: -34.397, lng: 150.644},

    zoom: 6

});

var infoWindow = new google.maps.InfoWindow({map: map});

// Try HTML5 geolocation.

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.');

        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.');

}

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
November 8, 2017

This forum is about JS in PDF files, not web-pages...

Known Participant
November 8, 2017

yes I now! It is possible to "translate" this code to PDF JavaScript? And take my current location?

try67
Community Expert
Community Expert
November 8, 2017

You can access external services using SOAP or even HTTP requests, but I don't see how you could use that to get your current location. Maybe through the IP address? But even then it's not going to be accurate.