android 拍照地理位置,Android 之 获取地理位置及监听

public class MainActivity extendsAppCompatActivity {private static final String TAG = "MainActivity";privateLocationManager locationManager;privateString locationProvider;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

locationManager=(LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);//下面注释的代码获取的location为null,所以采用Criteria的方式。

/*List providers = locationManager.getProviders(true);

if (providers.contains(LocationManager.GPS_PROVIDER)) {

//如果是GPS

locationProvider = LocationManager.GPS_PROVIDER;

Log.d(TAG, "onCreate: gps=" + locationProvider);

} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {

//如果是Network

locationProvider = LocationManager.NETWORK_PROVIDER;

Log.d(TAG, "onCreate: network=" + locationProvider);

} else {

Log.d(TAG, "onCreate: 没有可用的位置提供器");

Toast.makeText(this,"没有可用的位置提供器",Toast.LENGTH_SHORT).show();

return;

}

//获取Location,老是获取为空!所以用locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(locationProvider);*/Criteria criteria= newCriteria();

criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,如果设置为高精度,依然获取不了location。

criteria.setAltitudeRequired(false);//不要求海拔

criteria.setBearingRequired(false);//不要求方位

criteria.setCostAllowed(true);//允许有花费

criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗//从可用的位置提供器中,匹配以上标准的最佳提供器

locationProvider = locationManager.getBestProvider(criteria, true);if (ActivityCompat.checkSelfPermission(getApplicationContext(),

Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(),

Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED) {

Log.d(TAG,"onCreate: 没有权限 ");return;

}

Location location=locationManager.getLastKnownLocation(locationProvider);

Log.d(TAG,"onCreate: " + (location == null) + "..");if (location != null) {

Log.d(TAG,"onCreate: location");//不为空,显示地理位置经纬度

showLocation(location);

}//监视地理位置变化

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

}/*** LocationListern监听器

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

@Overridepublic void onStatusChanged(String provider, intstatus, Bundle arg2) {

}

@Overridepublic voidonProviderEnabled(String provider) {

Log.d(TAG,"onProviderEnabled: " + provider + ".." +Thread.currentThread().getName());

}

@Overridepublic voidonProviderDisabled(String provider) {

Log.d(TAG,"onProviderDisabled: " + provider + ".." +Thread.currentThread().getName());

}

@Overridepublic voidonLocationChanged(Location location) {

Log.d(TAG,"onLocationChanged: " + ".." +Thread.currentThread().getName());//如果位置发生变化,重新显示

showLocation(location);

}

};private voidshowLocation(Location location) {

Log.d(TAG,"定位成功------->"+"location------>经度为:" + location.getLatitude() + "

纬度为" +location.getLongitude());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值