HTML 5 geolocation POSITION UNAVAILABLE error in mobile

user1688401 picture user1688401 · Aug 7, 2013 · Viewed 10.8k times · Source

i am trying to get user current position in mobile web application my app work in all android telephone device except samsung galaksy s2 telephone device .. it give errror POSITION UNAVAILABLE error
this is demo link.you can view source this is code

navigator.geolocation.getCurrentPosition(handle_geolocation_query1, handle_errors) ;
       function handle_errors(error) {
            switch (error.code) {
                    case error.PERMISSION_DENIED: alert("user did not share geolocation data");
                        break;
                    case error.POSITION_UNAVAILABLE: alert("could not detect current position");
                        break;
                    case error.TIMEOUT: alert("retrieving position timed out");
                        break;
                    default: alert("unknown error");
                        break;
                }
            } 
       function handle_geolocation_query1(position) {



               $('#map_canvas').gmap('addMarker', {
                     'id':'m_1',
                    'position': new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
                    'bounds': true,
                    'icon': 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/geolocationmarker/images/gpsloc.png'
                }).click(function () {
           $('#map_canvas').gmap('openInfoWindow',{ 'content': '<font color="#2a2a2a" size="4">Location </font><br/><font color="#4a4a4a">Your current location</font>' }, this);
                });
                 var map = $('#map_canvas').gmap('get', 'map');
                map.setZoom(14);
                map.setCenter(new google.maps.LatLng(41.01802007732287, 28.971880674362183));
                map.setMapTypeId(google.maps.MapTypeId.ROADMAP);



            }

edited: i used phonegap to produce android apk file when i work this apk file application it give error POSITION UNAVAILABLE error but when i call this page from web it works it does not give error ... this is web link and you can download apk from here

Answer

Praveen picture Praveen · Aug 7, 2013

Your code is working fine in browser but not in mobile.

On checking your source code it seems you're specifying the sensor parameter as false.

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

You missed this while checking the documentation.

Specifying the Sensor Parameter

Use of the Google Maps API requires that you indicate whether your application is using a sensor (such as a GPS locator) to determine the user's location. This is especially important for mobile devices. Applications must pass a required sensor parameter to the tag when including the Maps API javascript code, indicating whether or not your application is using a sensor device.

Applications that determine the user's location via a sensor must pass sensor=true when loading the Maps API JavaScript.

Hope you understand.