本周通过继承高德api完成了地址定位
static String address = "定位失败"; private static final String[] authBaseArr = {//申请类型 Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }; private static final int authBaseRequestCode = 1; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu,menu); return true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_daohanglan); ActionBar bar = getSupportActionBar(); //自定义一个布局,并居中 bar.setDisplayShowCustomEnabled(true); View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.actionbar, null); bar.setCustomView(v, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT)); LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); initNavi(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling return; } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,//指定GPS定位提供者 1000,//指定数据更新的间隔时间 1,//位置间隔的距离为1m new LocationListener() {//监听GPS信息是否改变 @Override public void onLocationChanged(Location location) {//GPS信息发送改变时回调 Log.i("lgq","onLocationChanged===="+location.getProvider()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调 } @Override public void onProviderEnabled(String provider) { //定位提供者启动时回调 } @Override public void onProviderDisabled(String provider) { //定位提供者关闭时回调 } } ); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//获取最新的定位信息 if (location==null){ locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,//指定GPS定位提供者 5000,//指定数据更新的间隔时间 10,//位置间隔的距离为1m new LocationListener() {//监听GPS信息是否改变 @Override public void onLocationChanged(Location location) {//GPS信息发送改变时回调 Log.i("lgq","onLocationChanged===="+location.getProvider()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调 } @Override public void onProviderEnabled(String provider) { //定位提供者启动时回调 } @Override public void onProviderDisabled(String provider) { //定位提供者关闭时回调 } } ); location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//获取最新的定位信息 } locationUpdates(location); init(); initHomePage(); new Thread(new Runnable() { @Override public void run() { getWindow().getDecorView() .postInvalidate(); } }).start(); } private void initNavi() { if (android.os.Build.VERSION.SDK_INT >= 23) { if (!hasBasePhoneAuth()) { this.requestPermissions(authBaseArr, authBaseRequestCode); return; } } } private boolean hasBasePhoneAuth() { PackageManager pm = getPackageManager(); for (String auth : authBaseArr) { if (pm.checkPermission(auth, getPackageName()) != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } public void locationUpdates(Location location){ if(location != null){ StringBuilder stringBuilder = new StringBuilder(); //构建一个字符串构建器,用于记录定位信息 stringBuilder.append("经度:"); stringBuilder.append(location.getLongitude()); stringBuilder.append("\n纬度:"); stringBuilder.append(location.getLatitude()); String ab = getAddress(location.getLatitude(),location.getLongitude()); address=ab; } else{ address="GPS失效啦!"; } } public String getAddress(double latitude, double longitude) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) { Address address = (Address) ((List) addresses).get(0); String data = address.toString(); int startSpace = data.indexOf("\"") + ":".length(); int endSpace = data.indexOf("\"", startSpace); String space=data.substring(startSpace,endSpace); return space; } } catch (IOException e) { e.printStackTrace(); } return "获取失败!"; }
内容存在于fragmentui文件