0

我想获取当前位置的经纬度。我为此使用了 LocationManager,但它显示位置为空,纬度和经度为 0.0。我在清单和 GPS 连接中使用了所需的权限。

代码:

位置demoActivity.java

public class LocationdemoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);
        mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);

        Button buttonLocation = (Button) findViewById(R.id.buttonLocation);
        buttonLocation.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Toast.makeText(
                        getApplicationContext(),
                        "loc:" + MyLocationListener.location + "latitude "
                                + MyLocationListener.latitude + "longitude "
                                + MyLocationListener.longitude, 5000).show();

            }

        });

    }
}

MyLocationListener.java

 public class MyLocationListener implements LocationListener {

    public static double latitude;

    public static double longitude;
    public static Location location;

    @Override
    public void onLocationChanged(Location loc)
    {
        location = loc;
        loc.getLatitude();
        loc.getLongitude();
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
    }
    @Override
    public void onProviderDisabled(String provider)
    {
    }
    @Override
    public void onProviderEnabled(String provider)
    {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
    protected boolean isRouteDisplayed() {
        return false;
    }
}

清单中的权限:

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

问题是什么?

4

1 回答 1

0

尝试做这样的事情

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);

location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Button buttonLocation = (Button) findViewById(R.id.buttonLocation);

 buttonLocation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

              Toast.makeText(getApplicationContext(),
                            "loc:" + location + "latitude "
                                    + location.latitude + "longitude "
                                    + location.longitude, 5000).show();

                }

            });
于 2012-07-26T10:30:02.343 回答