Error "Failed to Start Geolocation service code: 2" on Android phone

1.4k views Asked by At

I am new to IBM Worklight.While using google map I need to get the current position of the device using gps.

I am not getting any error on emulator but in the phone getting the following alert

{"message":."Failed to start Geolocation service "," code":2}

The following code is using .

function alertGeo() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    function onSuccess(position) {
        codeLatLng(position.coords.latitude,position.coords.longitude);
    }
    function onError(error) {
        alert(JSON.stringify(error));
    }
}


function codeLatLng(lat,lng) {

      var latlng = new google.maps.LatLng(lat, lng);
      geocoder = new google.maps.Geocoder();
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
           alert("Address: " + results[1].formatted_address);

          } else {
            alert('No results found');
          }
        } else {
          alert('Geocoder failed due to: ' + status);
        }
      });
    }
1

There are 1 answers

0
Idan Adar On BEST ANSWER

Make sure you have the following permissions set in android\native\AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Also try adding the enableHighAccuracy: true option:

navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy: true});