Reputation: 175
I created an application which shows latitude and longitude position where is device. My question is how can I create something like IF method, that will show for example: If I'm between 46° and 48° longitude, and 52 and 54 latitude; program shows "This is the region 1!" example 2: I'm between 54° and 56° longitude, and 59° and 61° latitude; program shows "This is the region 2!" and so on... Any advices? Thanks a lot! here is the java code:
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");
}
}
Upvotes: 0
Views: 148
Reputation: 44118
Well using such multiple if statements just looks bad and is not the most efficient way, but if that floats your boat then sure:
@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));
}
Upvotes: 1