Recently I started using the Google Maps API, so I know very little about it.
When entering the site of my project the browser asks if it can obtain the location of the user. Only I noticed that the location varies depending on the browser. Using Google Chrome the location is fine however if accessing with Firefox or Edge is totally different ..
Why does this variation of location occur?
Google Chrome ( @ - 23.6058457, -46.6586903,18.25z )
Mozilla Firefox ( @ - 23.6083054, -46.6559814,18.4z )
Microsoft Edge ( @ - 23.5346485, -46.6277816,16.91z ) - Far from my location.
Is there anything I can change in my code to correct this inaccuracy?
var geocoder;
var map;
var marker;
function initialize() {
var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);
var options = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMaps"), options);
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
draggable: true,
icon: 'images/mapicon.png',
});
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));
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
marker.setPosition(initialLocation);
},
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!');
}