如何在android中获取纬度和经度?

2023-12-23

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

我尝试跟随this https://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobiledevice-in-android?answertab=active#tab-top但我无法运行程序。

My code

    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 清单中我把这个。

<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" />

请帮忙。 谢谢。


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

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 给出了您需要的纬度和经度值。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android中获取纬度和经度? 的相关文章

随机推荐