【问题标题】:How to get Latitude and Longitude in android?如何在android中获取纬度和经度?
【发布时间】:2012-06-30 08:23:45
【问题描述】:

android2.3.3如何获取当前经纬度?

我尝试关注this,但我无法运行程序。

我的代码

    LatView = (TextView)findViewById(R.id.LatText);
    LngView = (TextView)findViewById(R.id.LngText);
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();

我在 android manifest 中放了这个。

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

请帮忙。 谢谢。

【问题讨论】:

  • 无法运行?错误是什么?
  • 应用程序崩溃时发布 logcat。
  • 您是否在模拟器上进行测试,如果是,您需要进入 Eclipse 中的 DDMS 透视图,将位置坐标传递到模拟器中进行测试。它不会即时生成随机的。
  • 有时系统将getLastKnownLocation返回为null,因此location.getLongitude()会抛出空指针异常
  • 正如 jerryroberts 所说,模拟器需要你伪造某个位置。然而,ddms 的这个功能相当有问题,至少在我使用它的时候是这样,使用真正的设备是迄今为止使用 gps 时最好的。

标签: java android geolocation


【解决方案1】:

这是我在我的应用程序中找到设备当前位置的内容,它工作正常:

public class MyLocationActivity extends Activity implements LocationListener {
    private LocationManager mgr;
    private String best;
    public static double myLocationLatitude;
    public static double myLocationLongitude;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);

        dumpProviders();

        Criteria criteria = new Criteria();

        best = mgr.getBestProvider(criteria, true);
        Log.d("best provider", best);

        Location location = mgr.getLastKnownLocation(best);
        dumpLocation(location);

            //may be some intent here
    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        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

    }

    @Override
    protected void onPause() {

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

    @Override
    protected void onResume() {

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

    private void dumpLocation(Location l) {

        if (l == null) {

            myLocationLatitude = 0.0;
            myLocationLongitude = 0.0;
        } else {

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

    private void dumpProviders() {

        List<String> providers = mgr.getAllProviders();
        for (String p : providers) {

            dumpProviders(p);
        }
    }

    private void dumpProviders(String s) {

        LocationProvider info = mgr.getProvider(s);
        StringBuilder builder = new StringBuilder();
        builder.append("name: ").append(info.getName());
    }

}

这里 MyLocationLatitude 和 MyLocationLongitude 给出了您需要的纬度和经度值。

【讨论】:

  • 你在模拟器上试试这个吗?
  • 我在模拟器和手机上试用。
  • 在模拟器上它不起作用,它总是为当前设备位置的 lat 和 long 提供 0.0、0.0 ......但它在设备上工作吗?......如果不是,你必须在哪里调试它出错了,因为相同的代码对我来说很好
  • @Archie.bpgc 您必须通过远程登录“geo fix longitude latitude”命令为模拟器指定坐标,如下所述:stackoverflow.com/q/2279647/329079
  • +1 我喜欢让 android 选择最佳提供商的想法。
【解决方案2】:

您需要检查最后一个位置是否为空,如果是,则向 GPS 提供商询问新位置,如下所示:

public class Example extends Activity implements LocationListener {
    LocationManager mLocationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location if it is less than two minutes old
        }
        else {
            // Request a new location if necessary
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
        // Do something withthe current location

        // If you only want one location update un-comment this line
        //mLocationManager.removeUpdates(this);
        }
    }

    public void onProviderDisabled(String arg0) {}
    public void onProviderEnabled(String arg0) {}
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多