【问题标题】:LocationClient getLastLocation() returning nullLocationClient getLastLocation() 返回 null
【发布时间】:2013-07-27 13:23:27
【问题描述】:

我是 Android 编程新手,想从创建一个非常基本的应用开始,该应用在屏幕上显示当前位置的纬度和经度。

我正在为我的三星 Galaxy S3 进行开发,并了解到您需要 kickstart the phone to return the GPS

我已经尝试在 onConnected() 方法中使用 LocationManager 上的 requestLocationUpdates() 方法调用来执行此操作。但是我发现 LocationClient 仍然返回 null 位置。

谁能告诉我我做错了什么?

这是我的 MainActivity:

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;

public class MainActivity extends FragmentActivity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

    private LocationClient mLocationClient;
    private Location mCurrentLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLocationClient = new LocationClient(this, this, this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationClient.connect();
    }

    @Override
    protected void onStop() {
        mLocationClient.disconnect();
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onConnected(Bundle arg0) {
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        LocationManager manager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                new LocationListener() {
                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }

                    @Override
                    public void onLocationChanged(final Location location) {
                    }
                });
        mCurrentLocation = mLocationClient.getLastLocation();

        TextView latTextView = (TextView) findViewById(R.id.latitude_text);
        latTextView.setText(Double.toString(mCurrentLocation.getLatitude()));

        TextView longTextView = (TextView) findViewById(R.id.longitude_text);
        longTextView.setText(Double.toString(mCurrentLocation.getLongitude()));
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

}

我的 Android 清单如下所示:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />


    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.willigetwet.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

运行调试器时,我发现调用mLocationClient.getLastLocation() 后mCurrentLocation 为空。

如果有任何帮助,我将不胜感激!

【问题讨论】:

    标签: java android gps location


    【解决方案1】:

    mLocationClient.getLastLocation(); 可能会返回 null 如果您没有设备保存的任何最后位置。如果你阅读它的documentation,它会说:

    It returns the best most recent location currently available. If a location is not available, which should happen very rarely, null will be returned

    同样在onLocationChanged 方法中,当您的位置发生更改时,您可能希望将位置设置为最新位置。

    @Override
    public void onLocationChanged(final Location location) {
          mCurrentLocation = location;
    }
    

    【讨论】:

    • 就是这样 - 我没有保存任何最后的位置。前几天我得到了一个替换的 S3,完全忘记了我没有使用定位服务。一键点击谷歌地图设置 GPS,它现在工作正常 :)。谢谢!
    • @AnujSharma 改变了它。感谢您的评论。
    • @Shobhit 你能建议在这种情况下的工作吗?我的意思是我们应该怎么处理这个案子?
    • @SarahTattersall 你能分享你的解决方案吗?
    • getLastLocation() 从哪里获取位置坐标?从设备?如果是这样,我可以知道设备如何获取坐标吗?
    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 2015-08-15
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多