一.GPS定位一般需要这三个对象
LocationManager,LocationListener,Location
LocationMangager,位置管理器。要想操作定位相关设备,必须先定义个LocationManager。
LocationListener,位置监听,监听位置变化,监听设备开关与状态。
Location,位置信息,通过Location可以获取时间、经纬度、海拔等位置信息。
二.GPS实现的定位
(1)在AndroidManifest.xml配置权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
(2)在Java文件的实现代码
public class MainActivity extends AppCompatActivity {
//定位都要通过LocationManager这个类实现
private LocationManager locationManager;
private String provider;
@SuppressWarnings("static-access")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取定位服务
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//获取当前可用的位置控制器
List<String> list = locationManager.getProviders(true);
if (list.contains(LocationManager.GPS_PROVIDER)) {
//是否为GPS位置控制器
provider = LocationManager.GPS_PROVIDER;
} else {
Toast.makeText(this, "请检查网络或GPS是否打开", Toast.LENGTH_LONG).show();
return;
}
//权限检查
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(provider);
TextView tv1 = (TextView) this.findViewById(R.id.weather_title_cityname);
if (location != null) {
//获取当前位置,这里只用到了经纬度
String string = "纬度为:" + location.getLatitude() + ",经度为:"
+ location.getLongitude();
tv1.setText(string);
} else {
tv1.setText("定位失败");
}
//绑定定位事件,监听位置是否改变
//第一个参数为控制器类型第二个参数为监听位置变化的时间间隔(单位:毫秒)
//第三个参数为位置变化的间隔(单位:米)第四个参数为位置监听器
locationManager.requestLocationUpdates(provider, 2000, 2, locationListener);
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
//GPS状态变化时触发
}
@Override
public void onProviderEnabled(String arg0) {
//GPS禁用时触发
}
@Override
public void onProviderDisabled(String arg0) {
//GPS开启时触发
}
@Override
public void onLocationChanged(Location arg0) {
//位置信息变化时触发
}
};
//关闭时解除监听器
@Override
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
locationManager.removeUpdates(locationListener);
}
}
}
(3).xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/weather_title_cityname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="25dp"/>
<Button
android:id="@+id/manage_city_btn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_alignParentRight="true"
android:background="@drawable/menu2"/>
</RelativeLayout>
(4).运行结果
三.GPS定位获得城市信息,城市信息在result中,将result打印出来,可以根据需求获得想要的信息。
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> result = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
四.GPS注意事项
1.运行后等待好长时间才出现位置信息(我在室内,等了有半个小时吧,突然打开就出现结果了,不过网上大部分说GPS定位要在室外才能定位);
2.建议在开阔的地方测试
3.GPS定位准确,无网可以定位。有个缺点就是需要在开阔的地方使用。