Wrong location Google HTML API

Asked

Viewed 374 times

2

Hi, I’m trying to capture my location from the Google API/with the new HTML5 features.

However my latitude and longitude always gives this which is incorrect, whether on computer or mobile:

latitude: -23.550519899999998
longitude: -46.633309399999995

No mobile already enabled the location via GPS but even so it still brings the incorrect, which may be?

Follow the code I’m using:

<!DOCTYPE html>
<html>
    <body>
        <p id="demo">Aceite para receber sua localização em Latitude e Longitude</p>
        <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
        <script>
            var apiGeolocationSuccess = function(position) {
                alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
            };

            var tryAPIGeolocation = function() {
                $.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
                    apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
              })
              .fail(function(err) {
                alert("API Geolocation error! \n\n"+err);
              });
            };

            var browserGeolocationSuccess = function(position) {
                alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
            };

            var browserGeolocationFail = function(error) {
              switch (error.code) {
                case error.TIMEOUT:
                  alert("Browser geolocation error !\n\nTimeout.");
                  break;
                case error.PERMISSION_DENIED:
                  if(error.message.indexOf("Only secure origins are allowed") == 0) {
                    tryAPIGeolocation();
                  }
                  break;
                case error.POSITION_UNAVAILABLE:
                  alert("Browser geolocation error !\n\nPosition unavailable.");
                  break;
              }
            };

            var tryGeolocation = function() {
              if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(
                    browserGeolocationSuccess,
                  browserGeolocationFail,
                  {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
              }
            };

            tryGeolocation();
        </script>
        <!--<script>
            $(function(){
                getLocation();
            });
            var x = $("#demo");
            function getLocation(){
                if(navigator.geolocation){
                    navigator.geolocation.getCurrentPosition(showPosition);
                } else {
                    x.text("O seu navegador não suporta Geolocalização.");
                }
            }
            function showPosition(position){
                //console.log("Latitude: " + position.coords.latitude +" Longitude: " + position.coords.longitude);
                x.text("Latitude: " + position.coords.latitude +" Longitude: " + position.coords.longitude)
            }
        </script>-->
    </body>
</html>

It follows below as the answer of the code is coming: inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • Wrong? Where is this location? Very far from Voce ?

  • Yes! I don’t know if you’re from São Paulo, but I’m in pines and according to the coordinates, I’m in the center, square. https://goo.gl/maps/gk2Eq1AgZix

  • Strange indeed. I ran your code here and gave my location correctly.

  • I’ve run this code on three different devices and even two alternative scripts, but the answer is always the same! You know what can be??

  • @jvbarsou for me also came my correct location. Morumbi - SP. Already tried to run the script from another location?

  • @Rafaelaugusto performed some tests here outside the environment where I am but the range of proximity is still close mt and the answer remains the same. I added some images to the question showing how best it returns to me. Even if I click to search for the location directly on the google maps website it brings me to the square. I read about and they say that when the maps does not find you, it returns to see as "mark 0", because you know that you are in SP

  • Funny, I forwarded the link to a friend who is on the other side of sp and returned the same thing. i hosted the link in this domain, could you check out? cdz.bz/Geolocation.html

  • The google API itself does not provide this data accurately, since the Maps I worked with Waze has the lowest margin of error for this type of situation

  • @Lukssys clicking on the link he presented his correct location? cdz.bz/Geolocation.html

  • Just to not die question... Help!?

Show 5 more comments
No answers

Browser other questions tagged

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