gps定位

来源:互联网 发布:美工钢笔价格 编辑:程序博客网 时间:2024/04/30 19:46
private void getLocation() {// 获取位置管理服务String serviceName = Context.LOCATION_SERVICE;locationManager = (LocationManager) this.getSystemService(serviceName);// 查找到服务信息Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度criteria.setAltitudeRequired(false);criteria.setBearingRequired(false);criteria.setCostAllowed(true);criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息orientation();listener = new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {}@Overridepublic void onLocationChanged(Location location) {}};locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100 * 1000, 500, listener);}
private void orientation() {new Thread(new Runnable() {@Overridepublic void run() {while (truevalue == true) {try {Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // 通过GPS获取位置Message message = new Message();if (location == null) {message.what = 1;} else {message.what = 0;}hand.sendMessage(message);}}}).start();}Handler hand = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what == 1) {//show("gps无法定位");} }};

latitude = location.getLatitude();longitude = location.getLongitude();


0 0
原创粉丝点击