Android开发之--利用系统自带的服务获取经纬度并转为具体位置


//获取地理位置管理器
LocationManager mlocationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
List<String> prividerLists = mlocationmanager.getProviders(true);
if (prividerLists.contains(LocationManager.GPS_PROVIDER)) {
    locationProvider = LocationManager.GPS_PROVIDER;
} else if (prividerLists.contains(LocationManager.NETWORK_PROVIDER)) {
    locationProvider = LocationManager.NETWORK_PROVIDER;
} else {
    ToastUtil.showToastNew(this, "没有可用的位置提供器", 1);
    return;
}

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = mlocationmanager.getBestProvider(criteria,
        true);
Location location = mlocationmanager.getLastKnownLocation(locationProvider);
if (location != null) {
    updateWithNewLocation(location);
    //showLocation(location);
} else {
    tvMyAddress.setText("无法获取当前位置");

}
//监视地理位置变化
mlocationmanager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);

然后是上文中 updateWithNewLocation()方法

private void updateWithNewLocation(Location location) {
    String coordinate;
    String addressStr = "no address \n";
    if (location != null) {
        lat = location.getLatitude();
        lng = location.getLongitude();
        //double lat = 39.25631486;
        //double lng = 115.63478961;
        coordinate = "Latitude:" + lat + "\nLongitude:" + lng;
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat,
                    lng, 1);
            StringBuilder sb = new StringBuilder();
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    sb.append(address.getAddressLine(i)).append(" ");
                }
                /*sb.append(address.getCountryName());
                Log.i("location", "address.getCountryName()==" + address.getCountryName());//国家名*/
                sb.append(address.getLocality()).append(" ");
                Log.i("location", "address.getLocality()==" + address.getLocality());//城市名
                sb.append(address.getSubLocality());
                Log.i("location", "address.getSubLocality()=2=" + address.getSubLocality());//---区名
                addressStr = sb.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        //如果用户没有允许app访问位置信息 则默认取上海松江经纬度的数据
        lat = 39.25631486;
        lng = 115.63478961;
        coordinate = "no coordinate!\n";
    }
    Log.i("location", "经纬度为===" + coordinate);
    Log.i("location", "地址为====" + addressStr);
    tvMyAddress.setText(addressStr + "");
}

以下为图一中的locationListener

/**
 * LocationListern监听器
 * 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
 */
LocationListener locationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle arg2) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onLocationChanged(Location location) {
        //如果位置发生变化,重新显示
        showLocation(location);
    }
};


权限:


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Android移动开发中,Android设备可以利用光线传感器监测光照强度。光线传感器是一种测量环境光强度的传感器,它可以感知周围环境的光线水平。通过使用光线传感器,开发人员可以实现各种功能和应用。 要实现光线传感器的监测光照强度,首先需要在Android应用程序中引入相关的传感器API。然后,可以通过注册传感器事件监听器来监听光线传感器数据的变化。 一旦传感器数据发生变化,应用程序可以获取当前的光照强度值。这个值可以用来自动调节屏幕亮度、改变应用程序的界面布局或触发其他与光照有关的操作。 需要注意的是,不同的Android设备可能具有不同的传感器配置。因此,在开发应用时,需要考虑到设备的传感器支持情况,并进行适当的处理。 总结来说,Android设备利用光线传感器监测光照强度的实现需要引入传感器API,并注册传感器事件监听器来获取光照强度数据。这个功能可以用于各种应用,例如自动调节屏幕亮度或触发与光照有关的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Android 传感器概述](https://blog.csdn.net/qq_35427437/article/details/79936432)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值