Android 原生获取定位信息,定位经纬度

其实一般的方法就可以获取到经纬度,网上这样的例子也很多

方法一:

@SuppressLint("MissingPermission")
private static Location getLastKnownLocation(Activity activity) {
    LocationManager mLocationManager = (LocationManager) activity.getApplicationContext().getSystemService(LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            bestLocation = l;
        }
    }
    return bestLocation;
}

但是在一些做过手脚的Android系统设备就是不行,于是:

方法二:

@SuppressLint("MissingPermission")
private static void getLastKnownLocation2(Activity activity) {
    LocationManager mLocationManager = (LocationManager) activity.getApplicationContext().getSystemService(LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, locationListener);
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, locationListener);
}
private static LocationListener locationListener = new LocationListener() {
    @Override
    public void onStatusChanged(String provider, int status, Bundle arg2) {
        Log.d("cyp", "onStatusChanged: ");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("cyp", "onProviderEnabled: " + provider );
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("cyp", "onProviderDisabled: " + provider);
    }

    @Override
    public void onLocationChanged(Location location) {
        //如果位置发生变化,重新显示
        Log.d("cyp", "onLocationChanged:经度 "+location.getLongitude()+"纬度"+location.getLatitude());
        //这边就是获取到的经纬度,测试估计要4到5分钟才能获取得到,耐心点等等吧
     
    }
};

这边直接获取代码  LocationUtil.java-Java文档类资源-CSDN下载

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值