我想显示当前位置的纬度和经度。
为此,而不是在Google中进行搜索,而是在SO中进行了搜索。

我只想显示当前位置的纬度和经度。

请参阅下面的代码:

public class LocationGetter extends Activity {

@Override

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);

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

LocationListener mlocListener = new LocationManagerHelper();

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);

if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                + '\n');
        tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                + '\n');

        Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));

} else {
    tv.setText("GPS is not turned on...");
}

/** set the content view to the TextView */
setContentView(tv);

}

/* Class My Location Listener */

public static class LocationManagerHelper implements LocationListener {

    private static double latitude;
    private static double longitude;

    @Override
    public void onLocationChanged(Location loc) {
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
        Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
    }

    @Override
    public void onProviderDisabled(String provider) { }

    @Override
    public void onProviderEnabled(String provider) { }

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

    }

    public static double getLatitude() {
        return latitude;
    }

    public static double getLongitude() {
        return longitude;
    }

}

}


我还在清单文件中添加了权限:

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


看到我得到的输出:



我要去哪里错了?我不明白,它是一个简单的代码,为什么它不起作用?

最佳答案

我刚刚验证了您的代码,并且可以使用这些更改,您只需要将tv.append()调用移至每次调用的onLocationChanged()方法,如果您不将其用于CallBack,那么您将得到仅第一个设置值,并且仅执行一次。

        public void onLocationChanged(Location loc) {
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
            tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                    + '\n');
            tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                    + '\n');

            Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));

        }


我已经在AndroidManifest.xml中使用了这些权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />


您可以查看这些内容并过滤掉您不感兴趣的内容。我使用了从here下载的.gpx文件

我已经在Android 2.2上进行了测试,

关于android - 在Android中获取当前位置时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5579071/

10-12 03:33