位置管理器不工作没有互联网 [英] Location manager is not working without internet

查看:104
本文介绍了位置管理器不工作没有互联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是Java插件ñ我通过PhoneGap的要求。 但它给位置,只有当网络连接。我只需要使用GPS硬件,而不是通过互联网来获得位置。任何帮助??

This is java plugin n I am calling through phonegap . But It gives location only if internet is available. I need to get location only using gps hardware and not through internet . Any help??

我refferred一些与此相关的计算器问题。所以我就知道,GPS将不能在建筑物内的工作。好了,但在我的情况下,它是不是在外面打工也。之前,除非它没有得到最后的已知位置它不工作。一旦有最后一个位置一些如何,然后它开始工作。任何帮助??

I refferred some of the stackoverflow questions related to this. So i got know that GPS will not work inside the building . Ok , but in my case it is not working outside also. Until and unless it is not getting last known location it is not working . Once it has last location some how then it starts working . Any help ??

编辑:根据这个链接<一个href="http://stackoverflow.com/questions/14121197/android-trouble-in-getting-location-coordinates-by-only-using-gps-provider">Android - 麻烦越来越位置,只采用GPS提供商坐标,我可以使用libwlocate。但如何使用它作为PhoneGap的插件?任何帮助??

EDIT : According to this link Android - Trouble in getting location coordinates by only using GPS provider , i can use libwlocate . But how to use that as phonegap plugin ?? Any help ??

任何帮助是极大的AP preciated。

Any help is greatly appreciated.

编辑:怎么样,如果我把一些虚拟lastKnownLocation首次。然后,它会requestForNew一个正确的?

How about if i put some dummy lastKnownLocation for the first time. Then it will requestForNew one right??

推荐答案

您可以要求ONLY GPS像这样...

You can request "ONLY GPS" like this...

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

这篇关于位置管理器不工作没有互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆