洞察掌握android电视app开发中的安全与合规策略,提升企业运营效率
620
2022-12-01
Android定位获得经纬度(手机位置服务和百度定位)
private double latitude = 0.0; private double longitude = 0.0; //利用手机位置服务定位 void getLoaction() { final LocationManager locationManager = (LocationManager) getActivity() .getSystemService(Context.LOCATION_SERVICE); //创建一个criteria对象 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置不需要获取海拔方向数据 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); //设置允许产生资费 criteria.setCostAllowed(true); //要求低耗电 criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, false); Log.i(TAG, "we choose "+ provider); Location location = locationManager.getLastKnownLocation(provider); //第一次获得设备的位置 updateLocation(location); //重要函数,监听数据测试 locationManager.requestLocationUpdates(provider, 6000, 10, locationListener); } //创建一个事件- private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateLocation(location); } public void onProviderDisabled(String provider) { updateLocation(null); Log.i(TAG, "Provider now is disabled.."); } public void onProviderEnabled(String provider) { Log.i(TAG, "Provider now is enabled.."); } public void onStatusChanged(String provider, int status, Bundle extras) { } }; //获取用户位置的函数,利用Log显示 private void updateLocation(Location location) { Log.i(TAG, "updateLocation"); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); String oldLocation = MainUIActivity.mApplication.getLocation(); String newLocation = longitude + "," + latitude; mCurCommunityPoint.setText(newLocation); newLocation = Utils.bd_encrypt(latitude, longitude); Log.i(TAG, "The location has changed.. " + newLocation); if (!newLocation.equals(oldLocation)) { mCurPageIndex = 1; mHandler.obtainMessage(GET_COMMUNITY_DATA, newLocation).sendToTarget(); } } } //百度位置服务,百度定位sdk-: void getBaiduLoaction(){ LocationClient mLocationClient = new LocationClient(getActivity()); mLocationClient.registerLocationListener( new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { onLocationChanged(location); } } ); //注册监听函数 LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式 option.setCoorType("gcj02");//返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms option.setIsNeedAddress(true);//返回的定位结果包含地址信息 option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向 mLocationClient.setLocOption(option); mLocationClient.start(); } /** * 当位置发生变化时触发此方法 * * @param location 当前位置 */ public void onLocationChanged(BDLocation location) { if (location != null) { // 显示定位结果 Log.d(TAG, location.getLongitude() + " " + location.getLatitude()); latitude = location.getLatitude(); longitude = location.getLongitude(); String oldLocation = MainUIActivity.mApplication.getLocation(); String newLocation = longitude + "," + latitude; mCurCommunityPoint.setText(newLocation); newLocation = Utils.bd_encrypt(latitude, longitude); Log.i(TAG, "The location has changed.. " + newLocation); if (!newLocation.equals(oldLocation)) { mCurPageIndex = 1; mHandler.obtainMessage(GET_COMMUNITY_DATA, newLocation).sendToTarget(); } } }
从准确度来说百度还是比较靠谱的。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~