【问题标题】:Network location provider not giving location android网络位置提供商不提供位置 android
【发布时间】:2013-04-15 11:06:31
【问题描述】:

我正在开发一个小型 android 应用程序,我想在其中使用网络提供商找出用户的当前位置。我通过以下方式尝试了这个,但它没有给我任何输出:

networklocationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener networklocationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        Log.i("********************************",
                "this is my network location " + location);
        String Location_text = "NETWORK LOCATION latitude:"
                + location.getLatitude() + " longitude:"
                + location.getLatitude();
        network_location.setText(Location_text);
    }

    public void onStatusChanged(String provider, int status,
            Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location
// updates
networklocationManager
        .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                networklocationListener);

我像这样在清单文件中授予权限

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

有什么我想念的吗?这是正确的方法吗?需要帮忙。谢谢...


public class MainActivity extends Activity implements LocationListener {

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager gpslocationManager;
    private LocationManager networklocationManager;
    private LocationManager networklocationManager1;
    private String provider;
    private TextView gps_location;
    private TextView network_location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gps_location = (TextView) findViewById(R.id.gps_location);
        network_location = (TextView) findViewById(R.id.network_location);
        networkLocation();
    }

    public void networkLocation() {
        networklocationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        LocationListener networklocationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                Log.i("********************************",
                        "this is my network location " + location);
                String Location_text = "NETWORK LOCATION latitude:"
                        + location.getLatitude() + " longitude:"
                        + location.getLatitude();
                network_location.setText(Location_text);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };
        networklocationManager
                .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                        networklocationListener);
    }
}

【问题讨论】:

  • 你是用模拟器还是真机?
  • 谢谢路西法的回复。我正在真实设备中尝试它。如果我使用提供者作为 gps,它会给我输出。如果我使用网络提供商,它不会提供输出。
  • 好的,你的设备有 SIM 卡吗?
  • 是的,我的设备有 SIM 并且有适当的网络范围。
  • 你能上传更多你的代码吗?

标签: android geolocation location-provider


【解决方案1】:

确保在设置中启用定位服务!那应该是问题所在。它可能被禁用(并且此设置通常会在设置中的位置和安全中找到)

让我知道它是否有效!

【讨论】:

  • 如果网络提供商是 GPS_PROVIDER,OP 正在获取 GPS,唯一的问题是 NETWORK_PROVIDER
  • 是的,如果我使用 gps 提供商而不是网络提供商,我会得到 OP,但如果我使用网络提供商,它不会提供 OP
【解决方案2】:

您的网络提供商是否已启用?

boolean network_enabled;
try {
    network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
   ex.printStackTrace();
}

【讨论】:

    【解决方案3】:

    希望这部分代码能帮助你从vogella中提取出来。

    public class ShowLocationActivity extends Activity implements LocationListener {
    
        private TextView latituteField;
        private TextView longitudeField;
        private LocationManager locationManager;
        private String provider;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            latituteField = (TextView) findViewById(R.id.TextView02);
            longitudeField = (TextView) findViewById(R.id.TextView04);
            // Get the location manager
            locationManager = (LocationManager) 
                    getSystemService(Context.LOCATION_SERVICE);
            // Define the criteria how to select the locatioin provider -> use
            // default
            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            // Initialize the location fields
            if (location != null) {
                System.out.println("Provider " + provider + " has been selected.");
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
        }
    
        /* Request updates at startup */
        @Override
        protected void onResume() {
            super.onResume();
            locationManager.requestLocationUpdates(provider, 400, 1, this);
        }
    
        /* Remove the locationlistener updates when Activity is paused */
        @Override
        protected void onPause() {
            super.onPause();
            locationManager.removeUpdates(this);
        }
    
        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude());
            int lng = (int) (location.getLongitude());
            latituteField.setText(String.valueOf(lat));
            longitudeField.setText(String.valueOf(lng));
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    
        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

    • 同样的问题。如果我使用 gps 而不是网络,它会给出输出,但如果我使用网络不给出任何输出。我错过了什么。
    • 只有在位置改变时才会得到输出...您是否尝试移动和更改纬度和经度????只有在输入 onlocationchanged 时才会打印输出...您尝试过吗改变你的位置??
    • 您还需要移动相当多的位 - 网络位置非常不准确。将电话从一个房间移动到另一个房间是行不通的。您需要移动很长一段距离,就像在附近散步一样。
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2013-12-19
    相关资源
    最近更新 更多