android gps定位

mac2024-11-02  12

参考: https://blog.csdn.net/diyangxia/article/details/90674393#2_26

//网络定位有两种方式,一种是WIFI小区定位、另一种方式是基站定位 //GPS定位依赖于太空中的卫星 public class MyLocation extends Activity{ LocationManager locationManager; long minSecond = 2 * 1000; long minDistance =0; //2; Location location,lastLocation; TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); textview=(TextView)findViewById(R.id.textview); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (!(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) { Toast.makeText(this, "请打开网络或GPS定位功能!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, 0); return; } location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } if(location==null) Toast.makeText(getApplicationContext(), "没有存储上一次经纬度", Toast.LENGTH_SHORT).show(); updateLocation(location); //指定GPS定位的提供者 //间隔时间 //位置间隔1米 //打开gps后手机右上角有gps图标,gps与卫星连接成功后才会每隔minSecond毫秒回调locationListener locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minSecond, minDistance, listener);//LocationManager.GPS_PROVIDER } public final LocationListener listener = new LocationListener() {//监听GPS定位信息是否改变 @Override public void onLocationChanged(Location location) { //GPS信息发生改变时,回调 updateLocation(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { //GPS状态发生改变时,回调 switch (status) { // GPS状态为可见时 case LocationProvider.AVAILABLE: Toast.makeText(getApplicationContext(), "当前GPS状态为可见状态", Toast.LENGTH_SHORT).show(); Log.i("xml", "当前GPS状态为可见状态"); break; // GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Toast.makeText(getApplicationContext(), "当前GPS状态为服务区外状态", Toast.LENGTH_SHORT).show(); Log.i("xml", "当前GPS状态为服务区外状态"); break; // GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Toast.makeText(getApplicationContext(), "当前GPS状态为可见状态", Toast.LENGTH_SHORT).show(); Log.i("xml", "当前GPS状态为可见状态"); break; } } @Override public void onProviderEnabled(String provider) { //定位提供者启动时回调 //@SuppressLint("MissingPermission") Location location = locationManager.getLastKnownLocation(provider); updateLocation(location); } @Override public void onProviderDisabled(String provider) { //定位提供者关闭时回调 } }; private void updateLocation(Location location) { String result; double lat; double lon; if (location != null) { lat = location.getLatitude(); lon = location.getLongitude(); result = "纬度:" + lat + "\n经度:" + lon; Log.i("xml", "显示结果 = " + result); lastLocation = location; } else { result = "无法获取经纬度信息"; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormatDate = new SimpleDateFormat("yyyyMMdd"); String curTime = dateFormat.format(new Date()); String curDate = dateFormatDate.format(new Date()); // 把结果展示在界面上 textview.setText(curTime + "\n" + result); } }
最新回复(0)