私と私の職場との間の距離を計算するプログラムを書きました。アルゴリズムは非常に単純です。自分の位置の緯度と経度を取得し、それを Location オブジェクトに格納します。次に、別の Location オブジェクトを作成します。このオブジェクトには、作業場所の緯度と経度が格納されます。次に、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 km です。しかし、しばらくすると、距離は 2.4 km に減少し、しばらくすると 5.6 km に増加しました!:):) アイデアはありますか? 電話は私のテーブルの上にありますが、まったく動かしません。
ありがとう