Android - 位置未更新
Android - location not updating
在我使用 geo fix lng lat 命令时,我的应用程序没有发现位置已更改。我的简化代码如下所示:
if (googleApiClient.isConnected()) {
Location lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastKnownLocation!= null){
Log.d("Log", "Lat: " + lastKnownLocation.getLatitude());
Log.d("Log", "Lng: " + lastKnownLocation.getLongitude());
} else {
Log.d("Log", "No location detected");
}
}
googleApiClient:
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
因此 activity 包含该代码 运行 我可以发送命令 "geo fix 10 10" 并重新加载该代码(通过刷新按钮)但它仍然会记录
"No location Detected"
但是,如果我在模拟器中打开 Google 地图应用程序,然后返回我的应用程序并再次点击刷新,它将输出:
"Lat: 10"
"lng: 10"
似乎打开地图应用程序会触发应用程序位置的某种更新,而 geo fix 不会?
谢谢
因此,在对此处的文档和其他问题进行了一些挖掘之后,我发现我实际上根本没有在应用程序中更新位置。 (这是 Google 地图在我打开它时所做的,因此触发了要设置的新位置)
现在可以使用此实现(当应用程序位于前台时):
protected GoogleApiClient googleApiClient;
protected LocationRequest locationRequest;
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
然后在覆盖的 onConnected 方法中:
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest, this);
}
另外需要 onLocationChanged 方法,因为 class 现在正在实施 LocationListener
@Override
public void onLocationChanged(Location location) {
//Whatever is required when the location changes
}
在我使用 geo fix lng lat 命令时,我的应用程序没有发现位置已更改。我的简化代码如下所示:
if (googleApiClient.isConnected()) {
Location lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastKnownLocation!= null){
Log.d("Log", "Lat: " + lastKnownLocation.getLatitude());
Log.d("Log", "Lng: " + lastKnownLocation.getLongitude());
} else {
Log.d("Log", "No location detected");
}
}
googleApiClient:
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
因此 activity 包含该代码 运行 我可以发送命令 "geo fix 10 10" 并重新加载该代码(通过刷新按钮)但它仍然会记录 "No location Detected"
但是,如果我在模拟器中打开 Google 地图应用程序,然后返回我的应用程序并再次点击刷新,它将输出: "Lat: 10" "lng: 10"
似乎打开地图应用程序会触发应用程序位置的某种更新,而 geo fix 不会?
谢谢
因此,在对此处的文档和其他问题进行了一些挖掘之后,我发现我实际上根本没有在应用程序中更新位置。 (这是 Google 地图在我打开它时所做的,因此触发了要设置的新位置)
现在可以使用此实现(当应用程序位于前台时):
protected GoogleApiClient googleApiClient;
protected LocationRequest locationRequest;
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
然后在覆盖的 onConnected 方法中:
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest, this);
}
另外需要 onLocationChanged 方法,因为 class 现在正在实施 LocationListener
@Override
public void onLocationChanged(Location location) {
//Whatever is required when the location changes
}