我创建了一个显示纬度和经度位置的应用程序。我的问题是如何创建像IF方法这样的东西,例如:如果我在46°和48°之间,52和54纬度之间;节目显示“这是地区1!” 例2:我的经度在54°到56°之间,纬度在59°和61°之间;节目显示“这是地区2!”等等...有什么建议吗?非常感谢! 这是java代码:
public class dvica extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView location;
String lat;
String provider;
protected String Latitude,Longitude;
protected boolean gps_enabled,network_enabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dvica);
location = (TextView) findViewById(R.id.tekst1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location lokacija1) {
location = (TextView) findViewById(R.id.tekst1);
location.setText("Latitude:" + lokacija1.getLatitude() + ", "
+ "Longitude:" + lokacija1.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int stanje, Bundle extras) {
Log.d("Latitude","status");
}
}
答案 0 :(得分:1)
使用这样的多个if语句看起来很糟糕并且不是最有效的方法,但是如果它漂浮在你的船上那么肯定:
@Override
public void onLocationChanged(Location lokacija1) {
location = (TextView) findViewById(R.id.tekst1);
double lat = lokacija1.getLatitude();
double lng = lokacija1.getLongitude();
if (lat > 52 && lat < 54 && lon > 46.0 && lon < 48.0) {
location.setText("Region 1");
} else if (lat > 54 && lat < 56 && lon > 48.0 && lon < 50.0) {
location.setText("Region 2");
} else {
location.setText("Unrecognized region");
}
Log.d("TAG", String.format("lat:%.4f lon:%.4f", lat, lng));
}