微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – LocationClient getLastLocation()返回null

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

我正在为我的三星galaxy S3开发并读取你需要kickstart the phone to return the GPS.

我已尝试在onConnected()方法中使用LocationManager上的requestLocationUpdates()方法调用.但是我发现LocationClient仍然返回一个空位置.

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

这是我的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,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为null.

我会非常感谢任何帮助!

解决方法

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

它返回当前可用的最新位置.
如果某个位置不可用,这种情况很少发生,则返回null

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

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

原文地址:https://www.jb51.cc/java/121006.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐