獲取經緯度和地理位置
阿新 • • 發佈:2018-12-12
這裡用的是Xutils獲取json資料的,網址也在裡面,想用自己的框架可以換成自己的
//經緯度
location();
private void location() { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String locationstring = judgeProvider(lm); Log.d("MainActivity_aaaa", locationstring); //獲取地理位置管理器 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //獲取所有可用的位置提供器 List<String> providers = locationManager.getProviders(true); if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(this, "沒有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } Location location = locationManager.getLastKnownLocation(locationProvider); if (location != null) { //獲取當前位置,這裡只用到了經緯度 String stringa = "緯度為:" + location.getLatitude() + ",經度為:" + location.getLongitude(); Log.d("MainActivity_aaaa", "緯度為:" + location.getLatitude()); Log.d("MainActivity_aaaa", "經度為:" + location.getLongitude()); RequestParams params = new RequestParams("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + location.getLatitude() + "," + location.getLongitude() + "&output=json&pois=1&ak=LFx9XKQxYKsBkKR4syBfRLy3P0zNHs1e"); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { //解析result Log.d("MainActivity_aaaa", result); result = result.substring(29, result.length() - 1); Log.d("MainActivity_aaaa", "=" + result); Gson gson = new Gson(); CityBean cityBean = gson.fromJson(result, CityBean.class); String formatted_address = cityBean.getResult().getFormatted_address(); Log.d("MainActivity_aaaa", "formatted_address:" + formatted_address); main_text_riqilocation.setText(formatted_address); } //請求異常後的回撥方法 @Override public void onError(Throwable ex, boolean isOnCallback) { Toast.makeText(MainActivity.this, "位置出現錯誤", Toast.LENGTH_SHORT).show(); } //主動呼叫取消請求的回撥方法 @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } locationManager.requestLocationUpdates(locationProvider, 2000, 2, locationListener); // http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=30.68093376455154,104.06552381979525&output=json&pois=1&ak=LFx9XKQxYKsBkKR4syBfRLy3P0zNHs1e // http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.922213,116.400739&output=json&pois=1&ak=LFx9XKQxYKsBkKR4syBfRLy3P0zNHs1e } private String judgeProvider(LocationManager locationManager) { 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(MainActivity.this, "沒有可用的位置提供器", Toast.LENGTH_SHORT).show(); } return null; }
implementation 'org.xutils:xutils:3.3.36'
implementation 'com.google.code.gson:gson:2.8.+'
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!--定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />