Android位置来自网络时没有GPS返回null

时间:2014-07-29 10:21:59

标签: android gps location

我有一个代码可以从GPS获取位置,但如果没有启用GPS,那么我想使用网络。 GPS部分到目前为止工作,但NETWORK部分返回null位置,为什么会这样?这是我的代码。

private static final long MINIMUNDIST = 1;
private static final long MINTME = 1000;
protected LocationManager locationMan;
protected Button mybutton;

mybutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            showCurrentLocation();
        }
    });

protected void showCurrentLocation(){
    boolean gpsOn = locationMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean netOn = locationMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    Location loc;
    if (gpsOn){
        loc = locationMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    }
    else if (netOn){
        loc = locationMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
    else{
        Toast.makeText(MainActivity.this, "BOth GPS and NETWORK are OFF", Toast.LENGTH_SHORT).show();
        loc = null;
    }
    if (loc != null){
        String latText = String.format("%1$s" ,  loc.getLatitude());
        String logText = String.format("%1$s" ,  loc.getLongitude());
        TextView latTView = (TextView) findViewById(R.id.textView1);
        TextView longtext = (TextView) findViewById(R.id.TextView01);
        latTView.setText("Latitude :" + latText);
        longtext.setText("Longtude :"  + logText);
    }
    else{
        Toast.makeText(MainActivity.this, "Null location?!!" , Toast.LENGTH_SHORT).show();
    }
}


private class MyLocationListener implements LocationListener {



            public void onLocationChanged(Location location) {


            }



            public void onStatusChanged(String s, int i, Bundle b) {



            }



            public void onProviderDisabled(String s) {



            }



            public void onProviderEnabled(String s) {


            }



        }

我的清单文件包含任务所需的所有权限

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

顺便说一句,我正在通过USB调试在真实手机上测试应用程序。

2 个答案:

答案 0 :(得分:1)

我最近也参与了GPS工作,我跟着this tutorial,它可以正常工作(你可能需要修复一些部分以使其他功能正常运行..)

public Location getLocation(){
    try{
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        //getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


        if(!isGPSEnabled){

        }else{
            this.canGetLocation = true;

            if(isNetworkEnabled){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,  this);
                Log.d("Network", "Network");

                if(locationManager != null){
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if(location != null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        speed = location.getSpeed();
                    }
                }
            }

            if(isGPSEnabled){
                if(location == null){


                    locationManager.requestLocationUpdates((LocationManager.GPS_PROVIDER), MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");

                    if(locationManager != null){
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if(location != null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }

                }
            }
        }

    }catch (Exception e){
        e.printStackTrace();
    }

    return location;
}

您必须将所需权限添加到AndroidManifest.xml文件,如下所示

   <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" />

答案 1 :(得分:0)

位置侦听器方法返回null,直到它在某个位置发现锁定。网络提供程序位置需要几秒甚至几分钟才能锁定某个位置,因此需要等到某个位置发生锁定。