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" />


评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值