0

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);
        }
      });
    }
4

1 回答 1

2

确保您在 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" />

也尝试添加enableHighAccuracy: true选项:

navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy: true});
于 2014-11-26T04:24:59.563 回答