有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android,如何在自制应用程序中显示活动GPS坐标

有没有代码可以让你在自制的应用程序中显示你的活动坐标?我的谷歌地图有问题,想看看我的活动坐标是什么。我更喜欢在我的应用程序ie中查看谷歌地图时显示它,就像第二层信息一样。谢谢大家


共 (1) 个答案

  1. # 1 楼答案

    您可以设置LocationListener

    在onLocationChanged回调中,您将收到一个位置。那你就可以了。getLatitude()和。从位置获取经度(),并将其应用于布局

    public class MyActivity extends MapActivity implements LocationListener {
    
        private LocationManager locationManager;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            requestFineAccuracyLocationUpdates();
        }
    
        private void requestFineAccuracyLocationUpdates() {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
    
            List<String> providers = locationManager.getProviders(true);
    
            for (String provider : providers) {
                locationManager.requestLocationUpdates(provider, 5000, 1000, this);
            }
        }
    
        @Override
        public void onLocationChanged(Location location) {
            /* You can get your latitude and longitude here, and do whatever. */
        }
    }