我正在开发Android应用程序,它将告知用户他在哪里(在哪个建筑物中),gps工作正常,但是为什么我在我的代码中使用范围因为建筑物彼此非常接近,这是不准确的,但是在34栋建筑中使用许多IF ELSE效率不高。 还有其他办法吗?
这是我的代码。 package com.example.gps;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps,gps2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(MainActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
if((latitude >=21.4886 && latitude <=21.4893)&& (longitude >=39.2454 && longitude <=39.2459))
// \n is for new line
//Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Your are in building 7 " , Toast.LENGTH_LONG).show();
else
if((latitude >=21.48899 && latitude <=21.4899)&& (longitude >=39.2456 && longitude <=39.2459))
Toast.makeText(getApplicationContext(), "Your are in building 8 " , Toast.LENGTH_LONG).show();
//}
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
}