Android MapActivity 无法检索位置数据

发布于 2024-12-06 09:46:03 字数 2301 浏览 1 评论 0原文

我遵循了 Android 的 HelloMapView 教程,并添加了一些代码来捕获我当前的位置信息并显示在文本视图中。

我已经在另一个程序中测试了这些代码并且它有效,它能够在启动该应用程序时显示我的经度和纬度数据。但是,当我将代码复制到此 MapActivity 中时,似乎无法获取位置数据。

这是代码的一部分:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new HelloItemizedOverlay(drawable, this);
        Button button1 = (Button)this.findViewById(R.id.button1);
        tv1 = (TextView) this.findViewById(R.id.textView1);
        button1.setOnClickListener(mScan);
        latitude = 1.3831625;
        longitude = 103.7727321;


        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            if (System.currentTimeMillis() - location.getTime() < 3000)
            {
                longitude = location.getLongitude();
                latitude = location.getLatitude();

                tv1.setText(Double.toString(longitude));

            }
        }
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener);

    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();

            tv1.setText(Double.toString(latitude));
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    };   

I have followed the HelloMapView tutorial for android, and added some code to capture my current location information and display in a textview.

I have tested these code in another program and it worked, it is able to display my longitude and latitude data when the launch that app. However when i copied the code into this MapActivity, it seems that i cannot get the location data.

Here's part of the code:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new HelloItemizedOverlay(drawable, this);
        Button button1 = (Button)this.findViewById(R.id.button1);
        tv1 = (TextView) this.findViewById(R.id.textView1);
        button1.setOnClickListener(mScan);
        latitude = 1.3831625;
        longitude = 103.7727321;


        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            if (System.currentTimeMillis() - location.getTime() < 3000)
            {
                longitude = location.getLongitude();
                latitude = location.getLatitude();

                tv1.setText(Double.toString(longitude));

            }
        }
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener);

    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();

            tv1.setText(Double.toString(latitude));
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    };   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-12-13 09:46:03

您是否在清单中添加了适当的权限?像这样

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

另外请检查您的 GPS 是否已启用或在您的设备上工作。

Did you added proper permissions in Manifest? Like this

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Also please check your gps is enabled or working on ur deive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文