Java 更新后如何获取位置?
我遵循一个教程,使用谷歌地图API获取最后一个已知位置,或者在没有位置的情况下请求位置更新 这是我的onConnected方法Java 更新后如何获取位置?,java,android,google-maps,Java,Android,Google Maps,我遵循一个教程,使用谷歌地图API获取最后一个已知位置,或者在没有位置的情况下请求位置更新 这是我的onConnected方法 @Override public void onConnected(Bundle bundle) { Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (location == null) { Locati
@Override
public void onConnected(Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.v(TAG, "old location" + location);
}
else {
handleNewLocation(location);
Log.v(TAG, "new location");
}
}
因此,第一个条件应该请求一个新位置。这个位置存储在哪里?我想它可能会执行handeNewLocation(代码如下)
我需要获取位置并将其存储为字符串,所以我的问题是我不知道确切的位置
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
正在保存新位置
这是舱单:
<?xml version="1.0" encoding="utf-8"?>
你需要打电话
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.v(TAG, "old location" + location);
在你打电话之前
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
然后,该位置将存储在
location
中。请求位置更新的调用就是这样做的,它请求位置更新
在您拨打该电话后,新的位置将进入onLocationChanged()
如果希望始终保存当前位置,可以使用成员变量进行保存
请注意,不需要像现在这样记录空位置,因此在位置为空的情况下删除日志项:
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
handleNewLocation(location);
Log.v(TAG, "new location");
}
然后更新handleNewLocation()中的成员变量:
然后,在onLocationChanged()
回调中调用handleNewLocation()
:
@Override
public void onLocationChanged(Location location) {
//location changed, update current location
handleNewLocation(location);
Log.v(TAG, "new location");
}
全班:
public class LocationActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
double currentLatitude; //added as member variable
double currentLongitude; //added as member variable
String TAG = "myapp";
String loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
@Override
protected void onPause(){
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
protected synchronized void buildGoogleApiClient() {
Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10);
mLocationRequest.setFastestInterval(10);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F);
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
handleNewLocation(location);
Log.v(TAG, "new location");
}
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
currentLatitude = location.getLatitude(); //update member variable
currentLongitude = location.getLongitude(); //update member variable
loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude);
Log.v(TAG, loc);
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location) {
//location changed, update current location
handleNewLocation(location);
Log.v(TAG, "new location");
}
}
然后我的条件就不起作用了,因为我正在测试location是否为null,并且我确实需要请求位置更新,或者我是否可以只使用location。你认为if语句是不必要的吗?是的,我已经做了这件事。我正在做这件事,位置是空的。不确定我做错了什么?确定我做错了。编辑:我刚刚添加了它(你没有错过任何东西)。
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
currentLatitude = location.getLatitude(); //update member variable
currentLongitude = location.getLongitude(); //update member variable
loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude);
Log.v(TAG, loc);
}
@Override
public void onLocationChanged(Location location) {
//location changed, update current location
handleNewLocation(location);
Log.v(TAG, "new location");
}
public class LocationActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
double currentLatitude; //added as member variable
double currentLongitude; //added as member variable
String TAG = "myapp";
String loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
@Override
protected void onPause(){
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
protected synchronized void buildGoogleApiClient() {
Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10);
mLocationRequest.setFastestInterval(10);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F);
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
handleNewLocation(location);
Log.v(TAG, "new location");
}
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
currentLatitude = location.getLatitude(); //update member variable
currentLongitude = location.getLongitude(); //update member variable
loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude);
Log.v(TAG, loc);
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location) {
//location changed, update current location
handleNewLocation(location);
Log.v(TAG, "new location");
}
}