获取经纬度和地理位置

这篇博客介绍了如何利用Xutils这个Java框架从JSON数据中获取经纬度和地理位置信息,提供了详细的步骤和相关链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 这里用的是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" />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值