【问题标题】:Android cannot retrieve locationAndroid 无法检索位置
【发布时间】:2015-01-20 00:31:24
【问题描述】:

我创建了以下代码来简单地获取最新的纬度和经度,而无需用户打开 GPS,而只需依靠 wifi 或网络进行定位。运行应用程序后,它只返回吐司“Lat 为 0.0,Long 为 0.0”。我知道这应该可以工作,因为我已经在此处下载了示例代码:https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample,并适用于我的代码。

我怎样才能让它工作?

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

    public static double lat;
    public static double lng;

    ViewPager viewPager = null;

    /**
     * Provides the entry point to Google Play services.
     */
    protected GoogleApiClient mGoogleApiClient;

    /**
     * Represents a geographical location.
     */
    protected Location mLastLocation;

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

        buildGoogleApiClient();

        String text = "The Lat is " + lat
                + " and Long is " + lng;

        Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();

        };

        // Assign list to actionbar
        actionBar.setListNavigationCallbacks(aAdpt, null); // DEPRACATED

    } 


    /**
     * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
     */
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

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

    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    /**
     * Runs when a GoogleApiClient object successfully connects.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        // Provides a simple way of getting a device's location and is well suited for
        // applications that do not require a fine-grained location and that do not need location
        // updates. Gets the best and most recent location currently available, which may be null
        // in rare cases when a location is not available.
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            lat = mLastLocation.getLatitude();
            lng = mLastLocation.getLongitude();
        } else {
            Toast.makeText(this,"No location detected", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.

    }


    @Override
    public void onConnectionSuspended(int cause) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.

        mGoogleApiClient.connect();
    }
}

}

【问题讨论】:

  • 请不要,不是另一个位置问题...
  • @Selvin 你知道为什么没有返回坐标吗?

标签: android geolocation


【解决方案1】:

“lat”、“lng”和“mLastLocation”在执行 onConnected() 之前未设置或有效。您需要了解此操作是异步 - 也就是说,该位置不会立即返回,而是稍后提供。如果您在制作该 toast 时测试“mLastLocation”是否为空,这将是显而易见的。

【讨论】:

  • 如何设置它以便在应用程序加载时设置坐标?
  • String text = "The Lat is " + lat + " and Long is " + lng; Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show(); 移动到onConnected { if (mLastLocation != null) { ... here... }} ... 我瞎了眼,没看到这个
  • @Sauron 简单的答案是你不能。位置会定期更新,如果没有应用程序请求,可能根本不会更新,因此只有在您的应用程序请求后才可用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多