1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
public class MainActivity extends AppCompatActivity { public LocationClient mLocationClient; private TextView positionText;
private MapView mapView; private BaiduMap baiduMap; private boolean isFirstLocate = true;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(new MyLocationListener()); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.bmapView); baiduMap = mapView.getMap(); baiduMap.setMylocationEnabled(true); positionText = (TextView) findViewById(R.id.position_text_view); List<String> permissionList = new ArrayList<> (); if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if(!permissionList.isEmpty()) { String[] permissions = permissionList.toArray(new String[permissionList.size()]); ActivityCompat.requestPermissions(MainActivity.this, permission, 1); } else { requestLocation(); } }
@Override protected void onResume() { super.onResume(); mapView.onResume(); }
@Override protected void onPause() { super.onPause(); mapView.onPause(); }
@Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop(); mapView.onDestroy(); baiduMap.setMyLocationEnabled(false); }
private void navigateTo(BDLocation location) { if(isFirstLocate) { Latlng ll = new Latlng(location.getLatitude(), location.getLongtitude()); MapStatusUpdate update = MapStatusUpdateFactory.newLatlng(ll); baiduMap.animateMapStatus(update); update = MapStatusUpdateFactory.zoomTo(16f); baiduMap.animateMapStatus(update); isFirstLocate = false; } MyLocationData.Builder locationBuilder = new MyLocationData.Builder(); locationBuilder.latitude(location.getLatitude()); locationBuilder.longtitude(location.getLongtitude()); MyLocationData locationData = locationBuilder.build(); baiduMap.setMyLocationData(locationData); }
private void requestLocation() { initLocation(); mLocationClient.start(); }
private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setScanSpan(5000); option.setLocationMode(LocationClientOption.LocationMode.Device_Sensor);
option.setInNeedAddress(true); mLocationClient.setLocOption(option); }
@Override public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: if(grantResults.length > 0) { for(int result : grantResults) { if(result != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "必须同意所有权限才能使用本程序", Toast.LENGTH_SHORT).show(); finish(); return; } } requestLocation(); } else { Toast.makeText(this, "发送未知错误", Toast.LENGTH_SHORT).show(); finish(); } break; default: break; } } }
public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { StringBuilder currentPosition = new StringBuilder(); currentPosition.append("纬度:").append(location.getLatitude()).append("\n"); currentPosition.append("经度:").append(location.getLongtitude()).append("\n"); currentPosition.append("国家:").append(location.getCountry()).append("\n"); currentPosition.append("省:").append(location.getProvince()).append("\n"); currentPosition.append("市:").append(location.getCity()).append("\n"); currentPosition.append("区:").append(location.getDistrict()).append("\n"); currentPosition.append("街道:").append(location.getStreet()).append("\n"); currentPosition.append("定位方式"); if(location.getLocType() == BDLocation.TypeGpsLocation) { currentPosition.append("GPS 定位"); } else if(location.getLocType() == BDLocation.TypeNetWorkLocation) { currentPosition.append("网络定位"); } positionText.setText(currentPosition);
if(location.getLocType() == BDLocation.TypeGpsLocation || location.getLocType() == BDLocation.TypeNetWorkLocation) { navigateTo(location); } } }
|