How to get current user location through the google maps API?

Asked

Viewed 20,585 times

3

I am new to the development with this API and would like to show on the map that I am developing the current user position.

My code:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyB7s0zDs4RMWrpqWjAmr7OD-BQbq8LubZk&amp;sensor=false"></script>
<script>
var geocoder;
var map;
var marker;

  function initialize() {
    var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);
    var options = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("mapa"), options);

    geocoder = new google.maps.Geocoder();

    marker = new google.maps.Marker({
        map: map,
        draggable: true,
    });

    marker.setPosition(latlng);
}

  $(document).ready(function () {
    initialize();
});

1 answer

7

You can use the method getCurrentPosition() of the object Geolocation to pick up the user’s current location.

Example based on your code:

var geocoder;
var map;
var marker;

  function initialize() {
    var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);
    var options = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("mapa"), options);

    geocoder = new google.maps.Geocoder();

    marker = new google.maps.Marker({
        map: map,
        draggable: true,
    });

    marker.setPosition(latlng);
}

// verifica se o navegador tem suporte a geolocalização
if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position){ // callback de sucesso
        // ajusta a posição do marker para a localização do usuário
        marker.setPosition(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
    }, 
    function(error){ // callback de erro
       alert('Erro ao obter localização!');
       console.log('Erro ao obter localização.', error);
    });
} else {
    console.log('Navegador não suporta Geolocalização!');
}

$(document).ready(function () {
    initialize();
});

See working on Jsfiddle.

  • I could not get my current location ,I tested it in my code and also tested it in Jsfiddle and also failed!!.

  • @Hansmiller You have to allow the site to get its location when requested.

  • Pow sorry thanks! @Andre worked, how do I leave my map with the streets and streets of the place where I meet? why in the current form the map shows all Brazil. wanted that map that shows the streets. @Andre I am new in developing with this api vc can mi indicate sites ,video tutorials??

  • @Hansmiller Take a look here to see how to select a different map type. As for the tutorial, the guide to the documentation API is great and covers virtually everything you can do. Don’t forget to mark the answer as accepted if you are satisfied.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.