java - 有了坐标,具体区域如何设置?

标签 java android google-maps

我创建了一个应用程序,显示设备所在位置的纬度和经度位置。我的问题是如何创建类似 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/

相关文章:

java - JMS 出站 channel 适配器基于 java 的配置

java - NoSuchMethodException 取决于执行时间

Android:AdMob View 中断游戏 handle 的输入(onKey 事件停止工作)

android - 跟踪地铁 map

google-maps - 街景占位符在这里,但街景小人不见了

android - fragment 中的错误非默认构造函数

java - 选择编辑部分时,GEF : Is EditPart. PerformRequest(Request) 未调用?

java - 没有 SQL 的 Javafx 数据库

java - 如何在Android上创建透明演示?

android - 如何在 onNavigationItemSelected(MenuItem item) 下调用 Android Studio 1.4 NavigationDrawer 模板中的 fragment