我写了一个程序来计算我和我工作地点之间的距离。算法非常简单。我得到了我的位置的纬度和经度,并将其存储在Location对象中。然后我创建了另一个Location对象,它存储了我工作位置的lat和lon。然后我使用distanceTo(...)方法这是程序。
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,GoogleApiClient.ConnectionCallbacks,LocationListener{
private static final int PLAY_SERVICES_CODE = 90;
private static final String TAG = "Location";
Double latitude;
Double longitude;
Location destination;
Location currentLocation;
LocationRequest lq;
private TextView myLoc;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLoc = (TextView)findViewById(R.id.myLocation);
if(checkPlayServices()){
initializeGoogleApiClient();
}
}
private void initializeGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.build();
}
private boolean checkPlayServices(){
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(result!= ConnectionResult.SUCCESS){
if(GooglePlayServicesUtil.isUserRecoverableError(result)){
GooglePlayServicesUtil.getErrorDialog(result,this,PLAY_SERVICES_CODE).show();
}
return false;
}
return true;
}
@Override
public void onConnected(Bundle bundle) {
Log.v(TAG,"onConnected");
//Location loc = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
LocationRequest lq = LocationRequest.create();
lq.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
lq.setFastestInterval(10000);
//lq.setInterval(10000);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,lq,this);
}
@Override
public void onConnectionSuspended(int i) {
Log.v(TAG,"connection has been suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v(TAG,"connection failed");
}
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if(googleApiClient.isConnected()){
googleApiClient.disconnect();
}
}
@Override
public void onLocationChanged(Location loc) {
Log.v(TAG,"connection has been suspended");
//39,165397,22,399429
if(loc!=null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
destination = new Location("");
destination.setLatitude(39.639142);
destination.setLongitude(22.334340);
currentLocation = new Location("");
currentLocation.setLatitude(latitude);
currentLocation.setLongitude(longitude);
float distance = currentLocation.distanceTo(destination);
String stringDistance = String.valueOf(distance);
String finalDistance = stringDistance.substring(3);
myLoc.setText(finalDistance + " km");
}else{
myLoc.setText("No location obtained");
}
}
@Override
protected void onResume() {
super.onResume();
if(googleApiClient.isConnected())
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,lq,this);
}
}
我得到的值是7.3公里。然而,过了一会儿,距离减少到2.4公里,过了一会儿,它增加到5.6公里!:) :)任何想法,为什么这个有趣的事情发生了?电话在我的桌子上,我根本不动它。
由于