Android获取当前位置(GPS和网络定位)

1、比较:

GPS准确度高但耗电多,网络定位耗电少但准确度低

2、代码

①添加权限:
AndroidManifest.xml:


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

注:因为手机问题没有测试好,不确定是否加访问网络权限


②Java代码:

public class MainActivity extends Activity {
    //定位都要通过LocationManager这个类实现
    private LocationManager locationManager;
    private String provider;

    @SuppressWarnings("static-access")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取定位服务
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //获取当前可用的位置控制器
        List list = locationManager.getProviders(true);

        if (list.contains(LocationManager.GPS_PROVIDER)) {
        //是否为GPS位置控制器
            provider = LocationManager.GPS_PROVIDER;
        } 
        else if (list.contains(LocationManager.NETWORK_PROVIDER)) {
        //是否为网络位置控制器
            provider = LocationManager.NETWORK_PROVIDER;

        } else {
            Toast.makeText(this, "请检查网络或GPS是否打开",
                    Toast.LENGTH_LONG).show();
            return;
        }
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            //获取当前位置,这里只用到了经纬度
            String string = "纬度为:" + location.getLatitude() + ",经度为:"
                    + location.getLongitude();
        }

//绑定定位事件,监听位置是否改变
//第一个参数为控制器类型第二个参数为监听位置变化的时间间隔(单位:毫秒)
//第三个参数为位置变化的间隔(单位:米)第四个参数为位置监听器        
locationManager.requestLocationUpdates(provider, 2000, 2,
                locationListener);

    }


    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

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

        }

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

        }

        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub
            // 更新当前经纬度
        }
    };
    //关闭时解除监听器
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
        }
    }

}

你可能感兴趣的:(android)