安卓开发中根据城市名获取经纬度

最近在做项目的时候要实现滴滴打车那种点击城市列表跳转地图的功能,项目中使用的是高德地图,这个功能就出现了要根据地名转换成经纬度的需求。
我是使用了两种方法来完成的。
1.使用了安卓API中的Geocoder感兴趣的可以去Android API中查看https://developer.android.com/reference/android/location/Geocoder.html
实现的代码:

    Geocoder geocoder = new Geocoder(getContext() Locale.CHINA);
    try {
        List<Address> addressList = geocoder.getFromLocationName(cityName, 5);
        Address address = addressList.get(0);
        double latitude = address.getLatitude();
        double longitude = address.getLongitude();

    } catch (IOException e) {
        e.printStackTrace();
    }

这样就能获取到经纬度了,一开始测试的时候是没有问题的,后来发现找不到匹配的地址,猜测是谷歌服务除了问题。所以才有了下面这种方法。
2.使用高德地图
需要在下载地图jar包的时候添加上搜索的那个功能才能使用
直接贴代码吧


  GeocodeSearch geocodeSearch = new GeocodeSearch(getContext());
    geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
        @Override
        public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        }

        @Override
        public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
            if (geocodeResult != null && geocodeResult.getGeocodeAddressList() != null && geocodeResult.getGeocodeAddressList().size() > 0) {
                GeocodeAddress geocodeAddress = geocodeResult.getGeocodeAddressList().get(0);
                double lantitude = geocodeAddress.getLatLonPoint().getLatitude();
                double longitude = geocodeAddress.getLatLonPoint().getLongitude();
                map.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(lantitude, longitude)));
            }
        }
    });

    GeocodeQuery geocodeQuery = new GeocodeQuery(cityName, cityName);
    geocodeSearch.getFromLocationNameAsyn(geocodeQuery);

“`好了这样就实现了这个功能了,虽然过程有点波折,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio,我们可以使用LocationManager和LocationListener来获取当前的经纬度。 首先,我们需要在AndroidManifest.xml文件添加相应的权限: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 然后,在你的Activity,在onCreate方法获取LocationManager的实例,并检查是否获得了权限: LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean isCoarseLocationEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); boolean isFineLocationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 然后,我们需要创建一个LocationListener来监听位置变化事件: LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // 当位置变化时触发该方法 double latitude = location.getLatitude(); //获取纬度 double longitude = location.getLongitude(); //获取经度 // 将经纬度保存到变量,或者进行其他需要的处理 } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; 接下来,我们需要请求位置更新: if (isCoarseLocationEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } else if (isFineLocationEnabled) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } 最后,当不再需要获取位置信息时,要记得取消位置更新: locationManager.removeUpdates(locationListener); 这样,当我们点击按钮时,就能实时获得当前的经纬度了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值