【发布时间】:2016-03-24 11:46:30
【问题描述】:
您好,今天我打算在我的 Android 应用程序中实现位置,但遇到了一个我无法理解的不寻常问题。
问题
我首先查看了 Google 的官方文档以获取最后一个已知位置,但没有成功实施。然后我查看了提供的示例应用程序并将确切的代码复制到我自己的应用程序中,但这也失败了。该应用程序永远不会产生任何错误并且完全按照预期工作,但我根本没有获得位置。
在下面的代码中,mLastLocation 在我正在创建的应用程序中始终为空,但在谷歌制作的应用程序中完美运行。请注意,我和谷歌都使用这个确切的代码。
MainActivity
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
protected static final String TAG = "MainActivity";
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
/**
* Represents a geographical location.
*/
protected Location mLastLocation;
protected String mLatitudeLabel;
protected String mLongitudeLabel;
protected TextView mLatitudeText;
protected TextView mLongitudeText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mLatitudeLabel = getResources().getString(R.string.latitude_label);
mLongitudeLabel = getResources().getString(R.string.longitude_label);
mLatitudeText = (TextView) findViewById((R.id.latitude_text));
mLongitudeText = (TextView) findViewById((R.id.longitude_text));
buildGoogleApiClient();
}
/**
* 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) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel,
mLastLocation.getLatitude()));
mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel,
mLastLocation.getLongitude()));
} else {
Toast.makeText(this, R.string.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.
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
@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.
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
}
到目前为止我做了什么
- 确保按照其他线程中的建议打开 GPS 和 Wi-Fi。 (不工作)
- 已启动 Google 地图以强制使用其他线程中建议的位置。 (不工作)
- 下载了githubrepo并将核心文件复制到新项目中。 (不工作)
经过几个小时的挫折后,我从头开始创建了一个全新的项目,并尝试尽可能多地复制官方示例,但仍然无法正常工作。所以现在我尝试在下载后直接从谷歌运行示例代码,当然这就像一个魅力,但我不明白为什么。 我还花了最后 2 个小时试图弄清楚为什么 git repo 应用程序可以工作,以及为什么即使所有代码都完全相同,我自己的副本也不能工作。
我需要什么帮助
代码本身似乎没有错误,因为代码在示例 repo 应用程序中运行,并且没有产生任何错误。我需要有人告诉我我错过了什么!
为了帮助您,我使用我的两个 Android Studio 项目设置了一个 github 存储库。不起作用的项目称为BasicLocationSampleOwn,而起作用的项目称为BasicLocationSample。
你可以在这里下载 repo:https://github.com/CarlOhlsson/getLastLocationProblem
【问题讨论】:
标签: android android-service google-play-services android-location google-api-client