Android 定位地址,获取经纬度,并转换为中文地址
16lz
2021-01-23
Android 定位地址,获取经纬度,并转换为中文地址
- Android 定位地址,获取经纬度,并转换为中文地址
- 第一步,判断应用是否开启了位置定位的权限
- 第二步,重写onRequestPermissionsResult方法来监控权限的变化。
- 第三步,写一个方法来获取当前手机能提供的位置提供器。
- 第四步,写一个方法来定位的位置。
- 第五步,把获取到的经纬度转换为中文的具体地址。
- 第六步,写一个方法来接收返回的中文地址。
- 最后一步,当然我定位到地址后就执行了自己要执行的操作。
Android 定位地址,获取经纬度,并转换为中文地址
不用百度和谷歌等api,直接获取经纬度,并根据获取到的经纬度转换为中文地址
第一步,判断应用是否开启了位置定位的权限
首先判断应用是否开启了位置定位的权限(没有开启时会弹出开启权限的提示框,若已经开启权限,就执行后续的操作):
/** * 检测位置权限 * @param activity */ public static void checkLocationPermission(Activity activity){ Context context = activity; //获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限) if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //请求权限 ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE); } else { JudgeIMLine(context); } }
第二步,重写onRequestPermissionsResult方法来监控权限的变化。
当用户权限变化时,此回调方法会被调用:
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case LOCATION_CODE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { // 权限被用户同意。 Util.JudgeIMLine(this); } else { } break; } }
第三步,写一个方法来获取当前手机能提供的位置提供器。
具体方法实现如下:
/** * 获取位置提供器 * @param locationManager * @return */ public static String judgeProvider(LocationManager locationManager,Context context) { List<String> prodiverlist = locationManager.getProviders(true); if(prodiverlist.contains(LocationManager.NETWORK_PROVIDER)){ return LocationManager.NETWORK_PROVIDER;//网络定位 }else if(prodiverlist.contains(LocationManager.GPS_PROVIDER)) { return LocationManager.GPS_PROVIDER;//GPS定位 }else { //Toast.makeText(context,"没有可用的位置提供器",Toast.LENGTH_SHORT).show(); } return null; }
第四步,写一个方法来定位的位置。
此方法返回Location对象,里面包含了经纬度等一系列信息
/** * 定位位置 * @return */ public static Location beginLocatioon(Context context) { //获得位置服务 LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); String provider = judgeProvider(lm,context); //有位置提供器的情况 if (provider != null) { //为了压制getLastKnownLocation方法的警告 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return null; } return lm.getLastKnownLocation(provider); }else{ //不存在位置提供器的情况 //Toast.makeText(context,"不存在位置提供器的情况",Toast.LENGTH_SHORT).show(); } return null; }
第五步,把获取到的经纬度转换为中文的具体地址。
由于我们获取到的Location只是一个经纬度是普通人无法识别的地址,此处需要进行转换。代码中addresses 获取到的地址格式为:
[{“adminArea”:“重庆市”,“countryCode”:“CN”,“countryName”:“中国”,“latitude”:29.52302920290576,“locale”:“zh_CN”,“locality”:“重庆市”,“longitude”:106.52634124832649,“maxAddressLineIndex”:10,“subLocality”:“九龙坡区”,“subThoroughfare”:"",“thoroughfare”:“万象润街”}]
具体实现代码如下:
/** * 将经纬度转换成中文地址 * * @param location * @return */ public static String getLocationAddress(Location location,Context context) { String countryCode = ""; Geocoder geoCoder = new Geocoder(context); try { List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses!=null&&addresses.size()>0){ Address address = addresses.get(0); countryCode = address.getCountryCode(); } } catch (IOException e) { countryCode = "CN"; e.printStackTrace(); } return countryCode; }
第六步,写一个方法来接收返回的中文地址。
注意:代码中在调用getLocationAddress方法进行转中文地址时,开了个线程。这里必须要开线程,不然就无法进行转换。
/** * 判断登录线路 * * @return */ public static void JudgeIMLine(final Context context) { final Location location = beginLocatioon(context); if (location!=null){ final String[] countryCode = new String[1]; new Thread(new Runnable() { @Override public void run() { countryCode[0] = Util.getLocationAddress(location, context); if (countryCode!=null&&countryCode.length>0) { if ("CN".equals(countryCode[0])) { choiceCNLine(context); }else { //选择别的线路 } }else { choiceCNLine(context); } } }).start(); }else { choiceCNLine(context); } }
最后一步,当然我定位到地址后就执行了自己要执行的操作。
此处我获取到应用登录位置是中国,CN的时候就设置了中国的服务器地址。
/** * 选择国内线路 */ public static void choiceCNLine(Context context){ ShippingApp.getInstance().urlhost = context.getString(R.string.local_service_platform); ShippingApp.getInstance().urlChatHost = context.getString(R.string.chat_service_platform); ShippingApp.getInstance().phoneNum = context.getString(R.string.phoneNum); }
整个实现过程中需要注意的是,当位置权限没有开启的时候,judgeProvider此方法获取的数据是空的。在没有开线程的时候,getLocationAddress这个方法无法正常的把经纬度转换为中文具体地址。
当然,不要忘记在 AndroidManifest.xml文件中添加下面三个权限。
更多相关文章
- Android系统信息获取 之七:获取IP地址和MAC地址
- 自定义RadioButton样式并去除默认样式位置【Android】
- Android Studio 官方下载地址
- android取得所在位置的经纬度
- 自制Android下的播放器(音频来源SD卡上的固定位置)
- 在线安装eclipse cdt的地址
- android在线源码地址
- Android通过基站获取地理位置