这里用的是Xutils获取json数据的,网址也在里面,想用自己的框架可以换成自己的
Xutils连接:https://blog.youkuaiyun.com/qpc908694753/article/details/70463709
//经纬度
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" />