public void test(View v){
LocationManager lm = (LocationManager)TitleListActivity.this.getSystemService(Context.LOCATION_SERVICE);
//Criteria对象可以设置一系列查询条件,用于查询当前设备符合条件的LocationProvider
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度条件
criteria.setPowerRequirement(Criteria.POWER_LOW);//电量消耗低条件
criteria.setAltitudeRequired(false);//不需要海拔条件
criteria.setSpeedRequired(false);//不需要速度条件
criteria.setCostAllowed(false);//不予许产生费用条件
String provider = lm.getBestProvider(criteria, true);
Log.d("tab", provider);
//lm.requestLocationUpdates(定位类型, 多少时间更新一次, 多少米更新, 所绑定的监听)
TestLocationListener tll = new TestLocationListener();
lm.requestLocationUpdates(provider, 5000, 500, tll);
}
public class TestLocationListener implements LocationListener{
@Override//时间,地址满足其一执行该方法
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("tab", location.getLongitude()+"经度");
Log.d("tab", location.getLatitude()+"纬度");
Geocoder geocoder = new Geocoder(TitleListActivity.this,Locale.CHINA);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Iterator<Address> it= addresses.iterator();
while(it.hasNext()){
Address address = (Address)it.next();
//获取定位所在城市
String city = address.getLocality();
Log.d("tab", address+"");
Log.d("tab", city+"");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
定位方式的分类
1、GPS定位:
使用GPS卫星进行定位,需要加权限android.permission.ACCESS_FINE_LOCATION
2、NETWORK定位
使用信号接收塔和WIFI介入点进行定位,需要加权限
android.permission.ACCESS_FINE_LOCATION(精确定位)或
android.permission.ACCESS_COARSE_LOCATION(粗糙定位)