2020-09-03

tech2022-12-14  131

android  原生获取经纬度

/** * 定位:得到位置对象 * * @return */ private void getLocatin() { double latitude = 0.0; double longitude = 0.0; LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); List<String> providers = locationManager.getAllProviders(); Log.e("TAG", "Location" + providers.toString()); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { //从gps获取经纬度 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // to handle the case where the user grants the permission. See the documentation ToastUtil.showShort("没有GPS权限"); return; } Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, null); } else {//当GPS信号弱没获取到位置的时候又从网络获取 Log.e("Location", "网络位置信息" + getLngAndLatWithNetwork(this)); } } else { //从网络获取经纬度 // locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 000, 0, locationListener); locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, null); //通过查阅资料好像我们的Android手机要开启过GPS(第一次获取须GPS获取或者其他第三方应用开启使用过GPS)后然后getLastKnownLocation(LoactionManager.NETWORK_PROVIDER)这个方法才不会返回空。 Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } else { getLngAndLatWithNetwork(this); } } Log.e("TAG", "经纬度:" + longitude + "," + latitude); } //从网络获取经纬度 public String getLngAndLatWithNetwork(Context context) { double latitude = 0.0; double longitude = 0.0; LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return "没有网络Internet获取权限"; } // locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener); locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, null); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } else { // locationManager.requestSingleUpdate(LocationManager.PASSIVE_PROVIDER, locationListener, null); // locationManager.requestLocationUpdates(provider, 10000, 0, locationListener); location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } return longitude + "," + latitude; }

 

       

    private class MyLocationListener implements LocationListener {         private String latLongString;

        //当定位位置改变的调用的方法         //Location : 当前的位置         @Override         public void onLocationChanged(Location location) {             float accuracy = location.getAccuracy();//获取精确位置             double altitude = location.getAltitude();//获取海拔             final double latitude = location.getLatitude();//获取纬度,平行             final double longitude = location.getLongitude();//获取经度,垂直             Log.e("Location", "longitude:" + longitude + "  latitude:" + latitude + "精确位置" + accuracy + "海拔" + altitude);             new Thread(new Runnable() {                 @Override                 public void run() {                     List<Address> addsList = null;                     Geocoder geocoder = new Geocoder(MainActivity.this);                     try {                         addsList = geocoder.getFromLocation(latitude, longitude, 10);//得到的位置可能有多个当前只取其中一个                         Log.e("Location", "11111111" + addsList.toString());                         Log.e("Location", "3333333333" + JsonUtils.objectToStr(addsList));                     } catch (IOException e) {

                        e.printStackTrace();                     }                     if (addsList != null && addsList.size() > 0) {                         for (int i = 0; i < addsList.size(); i++) {                             final Address ads = addsList.get(i);                             latLongString = ads.getLocality();//拿到城市 //                            latLongString = ads.getAddressLine(0);//拿到地址                             runOnUiThread(new Runnable() {                                 @Override                                 public void run() {                                     Log.e("Location", "222222222222" + latLongString + "22222" + ads.getAddressLine(0) + "22222" + ads.getAddressLine(1) + "22222" + ads.getAddressLine(4));                                     Toast.makeText(MainActivity.this, "当前所在的城市为" + latLongString + ads.getAddressLine(0) + ads.getAddressLine(4) + ads.getAddressLine(1), Toast.LENGTH_LONG).show();                                 }                             });                         }                     }                 }             }).start();         }  

 

最新回复(0)