簡體   English   中英

沒有互聯網,位置經理無法工作

[英]Location manager is not working without internet

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 - 只使用GPS提供商獲取位置坐標的麻煩 ,我可以使用libwlocate。 但是如何使用它作為phonegap插件? 任何幫助?

任何幫助是極大的贊賞。

編輯:如果我第一次放置一些虛擬的lastKnownLocation怎么樣。 然后它會請求新的一個嗎?

您可以像這樣請求“ONLY 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM