【问题标题】:Not getting current location没有得到当前位置
【发布时间】:2014-01-16 17:08:52
【问题描述】:

我正在尝试获取当前位置并将其 LAt 和 LON 发送到另一个班级。但是我的应用程序没有获取当前位置,它显示的是 LAST LOCATION。请帮助我了解如何使用 GPS 获取当前位置和网络提供商。

下面是代码:

public class location  implements LocationListener 
{
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;

 public void locUpdate(Context context)
 {

    mgr = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
   // criteria.setAccuracy(Criteria.ACCURACY_COARSE);

    best = mgr.getBestProvider(criteria, true);
     mgr.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,0,0,this);

  try{
     Location location = mgr.getLastKnownLocation(best);
    Intent i = new Intent();
    i.setClass(context,sentsms.class);
     i.putExtra("latitude", location.getLatitude());
     i.putExtra("longitude", location.getLongitude());
     i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

     // Launch the new activity and add the additional flags to the intent
     context.getApplicationContext().startActivity(i);
    }
    catch(Exception e)
    {

    }
 }  





public void onLocationChanged(Location location) {

    dumpLocation(location);
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


protected void onPause() 
{

   // super.onPause();
    mgr.removeUpdates(this);
}

protected void onResume() {

   // super.onResume();
    mgr.requestLocationUpdates(best, 15000, 10, this);
}

private void dumpLocation(Location l) {

    if (l != null){

        myLocationLatitude = l.getLatitude();
        myLocationLongitude = l.getLongitude();
    }
}
} 

【问题讨论】:

    标签: android currentlocation


    【解决方案1】:

    来自getLastKnownLocation() 的文档:

    返回一个 Location 指示来自最后一个已知位置的数据 从给定的提供程序获得的修复。

    这可以在不启动提供程序的情况下完成。请注意,这 位置可能已过时,例如,如果设备已转动 关闭并移动到另一个位置。

    如果提供者当前被禁用,则返回 null。

    这意味着getLastKnownLocation() 将根据Provider 的最后修复来检索Location。如果你想更新Locations,请使用onLocationChanged()中收到的Location

    【讨论】:

      【解决方案2】:

      请试试这个方法

      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      // Creating a criteria object to retrieve provider
      Criteria criteria = new Criteria();
      // Getting the name of the best provider
      String provider = locationManager.getBestProvider(criteria, true);
      if (provider != null) {
      
      location = locationManager.getLastKnownLocation(provider);
      // locationManager.requestLocationUpdates(provider, 1000*60*60,
      // 1000, this);
      // update only in 1 hour and 1 km of distance
      locationManager.requestLocationUpdates(provider, 100, 1, this);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多