获取当前位置函数返回null

时间:2018-01-02 23:38:58

标签: android location locationlistener

以下代码是我的Android应用程序摘录以获取当前位置 我一直关注http://blog.teamtreehouse.com/beginners-guide-location-androidhttp://droidmentor.com/get-the-current-location-in-android/ 大多数文档甚至已经过时了developer.google和developers.android的内容。 我想知道为什么两个函数都返回null,我该怎么办呢 谢谢。

  @Override
public void onConnected(Bundle bundle) {
try
        {
            Log.d(TAG, "connection successful");

            mLastLocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);

            if (mLastLocation == null) {
                Log.d(TAG, "get last location = null");

               LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); //then nothing happens

            }
            else {
                Log.d(TAG, "last location successful");

                handleNewLocation(mLastLocation);
            }; }


@Override
public void onLocationChanged(Location location){ 
handleNewLocation(location);
}

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());

    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .title("I am here!");

    mMap.addMarker(options);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


    Address address = getAddress(location);

    starttext.setText("Current Location: \n" + address.toString());

}

1 个答案:

答案 0 :(得分:0)