位置管理器无法在没有互联网的情况下正常工作

3
public class NativeGeolocation extends Plugin {
  public long maximumAge = 1000 * 30; // ms
  public long timeout    = 1000 * 30; // ms
  public Location lastPosition = null; 
  public static final String ACTION_GETCURRENTPOSITION="getCurrentPosition";
  protected String callbackId = null;

  @Override
  public PluginResult execute(String action, JSONArray data, String callbackId)
  {
    JSONObject options = data.optJSONObject(0);
    Log.i("Myactivity","options : "+options);
    this.timeout    = timeout;

    this.callbackId = callbackId;
    Log.i("Myactivity","callbackId : "+this.callbackId);
    PluginResult result = new PluginResult(Status.NO_RESULT, callbackId);
    result.setKeepCallback(true);
    final LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria,false);
    if (ACTION_GETCURRENTPOSITION.equals(action)) {
      Log.i("Myactivity","inside getcurrentposition action");
      Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
      lastPosition = lastKnownLocation;
      Log.i("Myactivity","last location "+lastKnownLocation);
      if ((null != lastKnownLocation) && lastKnownLocation.getTime() + this.maximumAge > new Date().getTime()) {
        Log.i("Myactivity","inside b4 gotLocation");
        gotLocation(lastKnownLocation);
      } else {
        ctx.runOnUiThread(new RunnableLocationListener(this, callbackId, locationManager, provider));
      }
    } else {
      error(new PluginResult(Status.INVALID_ACTION), callbackId);
    }
    return result;
  }  

  public void gotLocation (Location location) {
    Log.i("Myactivity","inside  gotLocation");
    try {
      Log.i("Myactivity","inside  try");
      JSONObject geoposition = new JSONObject();
      JSONObject coords = new JSONObject();
      coords.put("latitude",         location.getLatitude());
      coords.put("longitude",        location.getLongitude());
      coords.put("altitude",         location.getAltitude());
      coords.put("accuracy",         location.getAccuracy());
      coords.put("altitudeAccuracy", null);
      coords.put("heading",          null);
      coords.put("speed",            location.getSpeed());
      geoposition.put("coords",    coords);
      geoposition.put("timestamp", location.getTime());
      geoposition.put("provider",  location.getProvider());
      geoposition.put("lastPos",  lastPosition);
      success(new PluginResult(Status.OK, geoposition), callbackId);
    } catch (JSONException jsonEx) {
      error(new PluginResult(Status.JSON_EXCEPTION), callbackId);
    }
  }

  protected String getBestProvider (LocationManager locationManager) {
    String provider = LocationManager.PASSIVE_PROVIDER;
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
      provider = LocationManager.GPS_PROVIDER;
    }
    else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
      provider = LocationManager.NETWORK_PROVIDER;
    }
    return provider;
  }

  class RunnableLocationListener extends Thread {
    protected final NativeGeolocation plugin;
    protected final LocationManager locationManager;
    protected final String provider;
    protected final String callbackId;
    public Boolean ended = null;
    public RunnableLocationListener (NativeGeolocation plugin, String callbackId, LocationManager locationManager, String provider) {
      Log.i("Myactivity","inside  runnabl");
      this.plugin = plugin;
      this.locationManager = locationManager;
      this.provider = provider;
      this.callbackId = callbackId;
    }

    public void run () {
      ended = false;
      PluginResult result = null;
      final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged (Location location) {
            Log.i("Myactivity","calling getlocation again1");
          if ( false == ended ) {
            Log.i("Myactivity","calling getlocation again2");
            plugin.gotLocation(location);
            locationManager.removeUpdates(this);
          }
        }
        public void onStatusChanged (String provider, int status, Bundle extras) {
          Log.i("Myactivity","inside onStatus Changed");
    }
        public void onProviderEnabled(String provider) {
          Log.i("Myactivity","inside onProvider Enabled");
    }
        public void onProviderDisabled(String provider) {
          Log.i("Myactivity","inside onProvider Disabled");
    }
      };
      locationManager.requestLocationUpdates(provider,1, 10, locationListener);//1 minutes and 10 meters
      Thread timeouter = new Thread() {
        public void run () {
          try {
            Thread.sleep(plugin.timeout);
            ended = true;
            locationManager.removeUpdates(locationListener);
            plugin.error(new PluginResult(Status.ERROR, "timeout"), callbackId);
          } catch (java.lang.InterruptedException ex) {
            error(new PluginResult(Status.JSON_EXCEPTION), callbackId);
          }
        }
      };
      timeouter.start();
    }
  }
}

这是我通过phonegap调用的java插件。但是只有在有互联网连接时才能获取位置。我需要仅使用GPS硬件获取位置,而不通过互联网。有什么帮助吗?

我参考了一些与此相关的stackoverflow问题。所以我知道GPS在建筑物内无法工作。好的,但在我的情况下,它在室外也无法工作。除非它获得了最后已知的位置,否则它就不会工作。一旦它有了最后的位置,它就开始工作了。有什么帮助吗?

编辑:根据此链接Android - Trouble in getting location coordinates by only using GPS provider ,我可以使用libwlocate。但如何将其用作phonegap插件?有什么帮助吗?

非常感谢任何帮助。

编辑:如果我第一次放置一些虚拟的lastKnownLocation,那怎么样?然后它会请求新的one,对吧?


1
请记住,GPS在建筑物内有时无法工作,因此请在阳台或其他地方尝试。 - Coderji
1
自从上周以来,我一直在测试使用GPS从家到办公室的路线,但是它对我来说不起作用。我已经搜索了很多文章,其中许多人都说如果你有GPS提供商的位置监听器并且没有连接互联网,它仍然可以工作,但是根据我上周的测试,我可以说没有互联网就无法工作。 - CoronaPintu
没有互联网就无法工作。有网络时我进行了测试,它可以正常工作,但准确度有所欠缺,但在互联网上它也可以工作。 - CoronaPintu
但是有没有不用互联网的方式来工作呢?只使用 GPS? - SSS
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/41587/discussion-between-pintu-corna-and-sss - CoronaPintu
显示剩余4条评论
1个回答

1
您可以这样请求“仅 GPS”:

...

LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
if(gpsProvider != null){
   locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 0, this);
}


Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Location lastLocation = null, gpsLocation = null;
if (isGPSEnabled) 
   gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接