我创建了一个应用程序,显示设备所在位置的纬度和经度位置。我的问题是如何创建类似 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");
}
}
最佳答案
使用这样的多个 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));
}
关于java - 有了坐标,具体区域如何设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25509338/