Skip to main content
Known Participant
Nov 8, 2017 4:15 PM UTC
Question

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

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 is closed to new replies. Start a new post to keep the conversation going.

4 replies

try67
Community Expert
Community Expert
Nov 8, 2017 4:16 PM UTC

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

Translate
Known Participant
Nov 8, 2017 4:41 PM UTC

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

Translate
try67
Community Expert
Community Expert
Nov 8, 2017 4:54 PM UTC

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.

Translate