作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个天气应用程序,除了获取用户的位置外,一切正常。我正在尝试使用播放服务 API 并调用 getLongitude() 和 getLatitude() 来做到这一点。但是,我正在编写的代码都没有工作,因为我在标题中列出的代码都没有运行。这是与获取位置有关的代码:
public class MainActivity extends ActionBarActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
public static final String TAG = MainActivity.class.getSimpleName();
private CurrentWeather mCurrentWeather;
private GoogleApiClient mGoogleApiClient;
private double mLongitude;
private double mLatitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Location services suspended. Please reconnect.");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Location services failed.");
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
最佳答案
您错过了初始化 GoogleApiClient
对象..
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
请添加以上代码并检查。
在 gradle 中使用这个库。 com.google.android.gms:play-services:8.4.0
请引用以下链接获取位置信息。
关于java - Android:onConnected、onConnectionSupspended 或 onConnectionFailed 都没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402029/
我正在制作一个天气应用程序,除了获取用户的位置外,一切正常。我正在尝试使用播放服务 API 并调用 getLongitude() 和 getLatitude() 来做到这一点。但是,我正在编写的代码都
我是一名优秀的程序员,十分优秀!