javascript geolocation on android doesn't work

前端 未结 4 859
春和景丽
春和景丽 2020-12-16 20:19

I am developing a website that relies on pulling the geolocation data of a mobile user. I am doing it the typical way via:

function initialize() {
   if(wind         


        
相关标签:
4条回答
  • 2020-12-16 21:04

    I was having this problem and I think it was because the power on the device was low. Charged it up and the problem went away.

    0 讨论(0)
  • 2020-12-16 21:08

    Is this being accessed from a WebView or from the Android Browser app? If from a WebView, you may need to enable geolocation via a Java call. See the WebView reference for that.

    Otherwise, I'm not sure precisely what's wrong with your JS, but here's HTML+JS that I know works with the Android browser:

    <!DOCTYPE html> 
    <html> 
    <head> 
      <script type="text/javascript"> 
    function watchLocation(successCallback, errorCallback) {
      successCallback = successCallback || function(){};
      errorCallback = errorCallback || function(){};
    
      // Try HTML5-spec geolocation.
      var geolocation = navigator.geolocation;
    
      if (geolocation) {
        // We have a real geolocation service.
        try {
          function handleSuccess(position) {
            successCallback(position.coords);
          }
    
          geolocation.watchPosition(handleSuccess, errorCallback, {
            enableHighAccuracy: true,
            maximumAge: 5000 // 5 sec.
          });
        } catch (err) {
          errorCallback();
        }
      } else {
        errorCallback();
      }
    }
    
    function init() {
      watchLocation(function(coords) {
        document.getElementById('test').innerHTML = 'coords: ' + coords.latitude + ',' + coords.longitude;
      }, function() {
        document.getElementById('test').innerHTML = 'error';
      });
    }
      </script> 
    </head> 
    
    <body onload="init()"> 
      <div id="test">Loading...</div> 
    </body> 
    </html>
    
    0 讨论(0)
  • 2020-12-16 21:12

    I have tested html5 geolocation functionality with the following two phones:

    • HTC Wildfire
    • HTC Galaxy

    Both were running the Android 2.1 update1 OS and both failed to do geolocation using the built in Google browser and demo sites like:

    • http://maxheapsize.com/static/html5geolocationdemo.html
    • http://html5demos.com/geo

    However, I have developed my own app (using a combination of the code already presented above) that successfully runs on:

    • HTC Wildfire
    • Android 2.1 update1 emulator
    • have not had HTC Galaxy yet to test

    and the code I am using is:

    1. Javascript lib:

      function is_browser_gps_capable() {
          var _locator_object;
          try {
              _locator_object = navigator.geolocation;
          } catch (e) { return false; }
          if (_locator_object)
              return true;
          else
              return false;
      }

      function watchLocation(successCallback, errorCallback) { successCallback = successCallback || function(){}; errorCallback = errorCallback || function(){};

      // Try HTML5-spec geolocation.
      var geolocation = navigator.geolocation;
      
      if (geolocation) {
          // We have a real geolocation service.
          try {
            function handleSuccess(position) {
              successCallback(position.coords);
            }
      
            geolocation.watchPosition(handleSuccess, errorCallback, {
              enableHighAccuracy: true,
              maximumAge: 5000 // 5 sec.
            });
          } catch (err) {
            errorCallback();
          }
        } else {
          errorCallback();
        }
      

      }

    2. actual code in my html file:

      if (is_browser_gps_capable()) {
          $('geo_location').innerHTML = 'Locating device...';
          watchLocation(function(coords) {
                var latlng = new google.maps.LatLng(coords.latitude, coords.longitude);
                var myOptions = {
                  zoom: 16,
                  center: latlng,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                var marker = new google.maps.Marker({
                    position: latlng, 
                    map: map,
                });
      $('geo_location').innerHTML = coords.latitude + ',' + coords.longitude; }, function() { $('geo_location').innerHTML = 'Not supported'; }); } else { $('geo_location').innerHTML = 'Not supported'; }

    Sorry this source code just does not want to format properly, message me and i'll send you the code directly if you wish so.

    0 讨论(0)
  • 2020-12-16 21:16

    This wasn't working on my Android 2.4, the problem was solved by enabling Wireless Networks Location besides GPS (wasn't working with just GPS)

    Settings -> Location & Privacy -> Enable Wireless Networks.

    0 讨论(0)
提交回复
热议问题