位置返回始终为空 - android [英] Location returns always null - android

查看:140
本文介绍了位置返回始终为空 - android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码的位置返回始终为空。请检查我的代码并告诉我什么是错的。当我在调试模式下运行项目时,provider =network但位置始终为空。我通过使用DDMS发送纬度和经度值,但不会改变。我不明白是什么问题

这部分在onCreate方法中

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        provider = lm.getBestProvider(criteria, true);
        location = lm.getLastKnownLocation(provider);

            updateLocation(location);

onResume方法中的这一部分

This part in the onResume method

@Override   
    protected void onResume() {  
        super.onResume();
        lm.requestLocationUpdates(provider, 5000, 10, this);
    }

我使用LocationListener(MapActivity实现LocationListener),这是onLocationChanged部分

I use LocationListener (MapActivity implements LocationListener) and this is onLocationChanged part

public void onLocationChanged(Location location) {
        updateLocation(location);   
    }

终于这是我的updateLocation()

private void updateLocation(Location location){
        if (location != null) {
            Double lat = location.getLatitude()*1E6;
            Double lng = location.getLongitude()*1E6;
            point = new GeoPoint(lat.intValue(), lng.intValue());

            mapView = (MapView) findViewById(R.id.mapView);
            mc = mapView.getController();
            mapView.setBuiltInZoomControls(true);

            //List<Overlay> overlays = mapView.getOverlays();
            myLocOverlay = new MyLocationOverlay(this, mapView);
            //overlays.add(myLocOverlay);
            mapView.getOverlays().add(myLocOverlay);
            myLocOverlay.enableMyLocation();

            myLocOverlay.runOnFirstFix(new Runnable() {
                public void run() {
                    mc.animateTo(myLocOverlay.getMyLocation());
                    }
            }); 

            mc.setZoom(15);
            mapView.invalidate();

            }

    }

我给了必要的权限,并使用Google地图库

I gave essential permissions and I use google maps library

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

<uses-library
            android:name="com.google.android.maps"
            android:required="true" >
        </uses-library>


推荐答案

我想你应该使用LocationManager.GPS_PROVIDER来请求。您可以测试我的代码:

I think u should use LocationManager.GPS_PROVIDER to request. You can test my code:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1l,1f, new LocationListener() {        
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
         // TODO Auto-generated method stub

    }

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

    }

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

    }

    @Override
    public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
        updateLocation(location);               
    }
    });

祝您好运!
^ - ^

Good luck! ^-^

这篇关于位置返回始终为空 - android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆