我怎样才能获得当前位置?Android Studio

Reh*_*saf 6 java gps android

我知道之前已经问过,但所有可用的解决方案都要求我使用onLocationChanged()方法,所以我可以使用getLatitude()getLongitude()方法来获取坐标.

但是onLocationChanged()当我的设备位置发生变化时会被调用.因此,为了获得我当前的位置,我将不得不移动我的设备.

但我想获得当前的坐标,而无需移动我的设备.此外,我读到某个地方,我可以onLocationChanged()手动调用该方法,我可以得到最后的已知位置,但再次,

是不是最后一个已知位置不可能是我当前的位置?或者可以使用最后一个已知的位置解决方案?

这是我用来实现LocationListener的类的一部分.

public class MyLocListener extends MapsActivity implements LocationListener {

    public void onLocationChanged(Location location)
    {
        if(location!=null)
        {
            MapsActivity.setLatitude(location.getLatitude());
            MapsActivity.setLongitude(location.getLongitude());
        }
    }}
Run Code Online (Sandbox Code Playgroud)

这是我用来为我的地图活动提供坐标的函数的覆盖.纬度和经度是两个双重类型变量.在这个类中,纬度和经度变量以0开始,并且它们保持这种方式,因为onLocationChanged()仅在我的位置改变时才调用该函数.因此,我无法在地图上看到我当前的位置.

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;


        LocationManager myLocManager;

        myLocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        MyLocListener loc = new MyLocListener();
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        myLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);

        LatLng sydney = new LatLng(latitude, longitude);

        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
Run Code Online (Sandbox Code Playgroud)

the*_*ndr 3

你应该使用 GooglePlayServices

compile 'com.google.android.gms:play-services-location:7.0.0'
Run Code Online (Sandbox Code Playgroud)

获取位置

 if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}
if (mGoogleApiClient != null) {
    mGoogleApiClient.connect();
}
Run Code Online (Sandbox Code Playgroud)

连接后

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请查看文档