| | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | |
| | | import com.amap.api.location.AMapLocation; |
| | | import com.amap.api.location.AMapLocationClient; |
| | | import com.amap.api.location.AMapLocationClientOption; |
| | | import com.amap.api.location.AMapLocationListener; |
| | | import com.amap.api.maps.AMap; |
| | | import com.amap.api.maps.CameraUpdate; |
| | | import com.amap.api.maps.CameraUpdateFactory; |
| | | import com.amap.api.maps.LocationSource; |
| | | import com.amap.api.maps.MapView; |
| | | import com.amap.api.maps.UiSettings; |
| | | import com.amap.api.maps.model.BitmapDescriptorFactory; |
| | | import com.amap.api.maps.model.CameraPosition; |
| | | import com.amap.api.maps.model.LatLng; |
| | | import com.amap.api.maps.model.LatLngBounds; |
| | | import com.amap.api.maps.model.Marker; |
| | | import com.amap.api.maps.model.MarkerOptions; |
| | | import com.amap.api.maps.model.MyLocationStyle; |
| | | import com.amap.api.maps.model.Poi; |
| | | import com.amap.api.maps.model.Polyline; |
| | | import com.amap.api.maps.model.PolylineOptions; |
| | | import com.amap.api.services.core.PoiItem; |
| | | import com.amap.api.services.help.Inputtips; |
| | | import com.amap.api.services.help.InputtipsQuery; |
| | | import com.amap.api.services.poisearch.PoiResult; |
| | | import com.amap.api.services.poisearch.PoiSearch; |
| | | import com.xianning.driver.R; |
| | | import com.xianning.driver.base.Const; |
| | | import com.xianning.driver.base.MyBaseActivity; |
| | |
| | | * |
| | | * @author 唐浩 |
| | | */ |
| | | public abstract class BaseMapActivity extends MyBaseActivity implements AMap.OnMyLocationChangeListener |
| | | , AMap.OnMapClickListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnPOIClickListener, |
| | | PoiSearch.OnPoiSearchListener, EasyPermissions.PermissionCallbacks, LocationSource, AMapLocationListener { |
| | | public abstract class BaseMapActivity extends MyBaseActivity{ |
| | | private static final String TAG = "BaseMapActivity"; |
| | | public AMap aMap = null; |
| | | private MyLocationStyle mMyLocationStyle = null; |
| | | public AMapLocationClient mlocationClient = null; //定位 |
| | | public AMapLocationClientOption mLocationOption = null;//定位 |
| | | public Bundle savedInstanceState;// |
| | | private MapView mapView; |
| | | public AMapLocation mAMapLocation;//实时定位的位置 |
| | | |
| | | /** |
| | | * 初始化地图 |
| | | */ |
| | | public void initMap(Bundle savedInstanceState, MapView mapView) { |
| | | this.savedInstanceState = savedInstanceState; |
| | | this.mapView = mapView; |
| | | if (checkPermission(Const.permissions_location_fine, Const.permissions_location_coarse, Const.permissions_write)) { |
| | | init(savedInstanceState, mapView); |
| | | initLocation(); |
| | | } |
| | | } |
| | | |
| | | public abstract boolean isShowBluePoint(); |
| | | |
| | | public void init(Bundle savedInstanceState, MapView mapView) { |
| | | if (checkPermission(Const.permissions_location_fine, Const.permissions_location_coarse, Const.permissions_write)) { |
| | | mapView.onCreate(savedInstanceState); |
| | | aMap = mapView.getMap(); |
| | | mMyLocationStyle = new MyLocationStyle(); |
| | | mMyLocationStyle.interval(5000); |
| | | mMyLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER); |
| | | mMyLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.gps_point)); |
| | | aMap.setMyLocationStyle(mMyLocationStyle); |
| | | aMap.setInfoWindowAdapter(this); |
| | | aMap.setMyLocationEnabled(isShowBluePoint()); |
| | | aMap.setOnMyLocationChangeListener(this); |
| | | aMap.setOnMapClickListener(this); |
| | | aMap.setOnMarkerClickListener(this); |
| | | // aMap.setLocationSource(this); |
| | | aMap.setOnPOIClickListener(this); |
| | | aMap.animateCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel() - 3)); |
| | | // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false |
| | | aMap.setMyLocationEnabled(true); |
| | | // 设置定位的类型为定位模式,有定位、跟随或地图根据面向方向旋转几种 |
| | | aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); |
| | | UiSettings uiSettings = aMap.getUiSettings(); |
| | | uiSettings.setZoomControlsEnabled(false); |
| | | } |
| | | } |
| | | |
| | | public void init(Bundle savedInstanceState, MapView mapView, boolean b) { |
| | | if (checkPermission(Const.permissions_location_fine, Const.permissions_location_coarse, Const.permissions_write)) { |
| | | mapView.onCreate(savedInstanceState); |
| | | aMap = mapView.getMap(); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 移动相机到某个位置 |
| | | */ |
| | | public void moveCamera(LatLng latlng) { |
| | | CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition( |
| | | new CameraPosition(latlng, 16f, 0f, 0f)); |
| | | aMap.animateCamera(mCameraUpdate); |
| | | } |
| | | |
| | | public void moveCamera(LatLng startLatlng,LatLng endLatlng) { |
| | | LatLngBounds bounds = new LatLngBounds(startLatlng, endLatlng); |
| | | CameraUpdate mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 250); |
| | | aMap.animateCamera(mCameraUpdate); |
| | | } |
| | | |
| | | /** |
| | | * 添加marker点 |
| | | */ |
| | | public Marker addMarker(double mLat, double mLng, View view, String peopleId) { |
| | | MarkerOptions options = getOption(mLat, mLng, view); |
| | | Marker marker = aMap.addMarker(options); |
| | | if (!TextUtils.isEmpty(peopleId)) { |
| | | marker.setObject(peopleId); |
| | | } |
| | | return marker; |
| | | } |
| | | |
| | | public MarkerOptions getOption(double mLat, double mLng, View view) { |
| | | return new MarkerOptions().anchor(0.5f, 1) |
| | | .position(new LatLng(mLat,mLng)) |
| | | .icon(BitmapDescriptorFactory.fromBitmap(convertViewToBitmap(view))).draggable(true).setFlat(true); |
| | | } |
| | | |
| | | |
| | | /*** |
| | | * |
| | | * @param str |
| | | * @param var1 |
| | | */ |
| | | public void searchSite(String str, Inputtips.InputtipsListener var1) { |
| | | InputtipsQuery inputquery = new InputtipsQuery(str, ""); |
| | | Inputtips inputTips = new Inputtips(this, inputquery); |
| | | inputTips.setInputtipsListener(var1); |
| | | inputTips.requestInputtipsAsyn(); |
| | | } |
| | | |
| | | /** |
| | | * 删除秒点外的所有marker |
| | | */ |
| | | public void deleteAllMarker() { |
| | | for (int i = 0; i < aMap.getMapScreenMarkers().size(); i++) { |
| | | Marker marker = aMap.getMapScreenMarkers().get(i); |
| | | if (marker.getObject() != null && !marker.getObject().toString().equals("集合位置")) { |
| | | Log.i(TAG, "deleteAllMarker: " + i); |
| | | marker.remove(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /*** |
| | | * 更具tag删除marker |
| | | * @param tag |
| | | */ |
| | | public void deleteMarker(String tag) { |
| | | for (int i = 0; i < aMap.getMapScreenMarkers().size(); i++) { |
| | | Marker marker = aMap.getMapScreenMarkers().get(i); |
| | | if (marker.getObject() != null && marker.getObject().toString().equals(tag)) { |
| | | marker.remove(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /*** |
| | | * 根据点 获取距离 |
| | |
| | | double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) |
| | | + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lng2 - lng1)) * ban * 1000; |
| | | return d; |
| | | } |
| | | |
| | | /*** |
| | | * 根据id获取marker |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public Marker getMarker(String id) { |
| | | for (int i = 0; i < aMap.getMapScreenMarkers().size(); i++) { |
| | | Marker marker = aMap.getMapScreenMarkers().get(i); |
| | | if (marker.getObject() != null && marker.getObject().toString().equals(id)) { |
| | | return marker; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /*** |
| | | * marker点击事件 |
| | | * @param marker |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean onMarkerClick(Marker marker) { |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /*** |
| | | * 显示marker窗口 |
| | | * @param marker |
| | | * @return |
| | | */ |
| | | @Override |
| | | public View getInfoContents(Marker marker) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public View getInfoWindow(Marker marker) { |
| | | return null; |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public void onMapClick(LatLng latLng) { |
| | | Log.i(TAG, "onMapClick: "); |
| | | } |
| | | |
| | | @Override |
| | | public void onMyLocationChange(Location location) { |
| | | Log.i(TAG, "onMyLocationChange: "); |
| | | } |
| | | |
| | | @Override |
| | | public void onPOIClick(Poi poi) { |
| | | Log.i(TAG, "onPOIClick: "); |
| | | } |
| | | |
| | | /*** |
| | | * search 搜索结果 |
| | | */ |
| | | @Override |
| | | public void onPoiSearched(PoiResult poiResult, int i) { |
| | | Log.i(TAG, "onPoiSearched: "); |
| | | } |
| | | |
| | | @Override |
| | | public void onPoiItemSearched(PoiItem poiItem, int i) { |
| | | Log.i(TAG, "onPoiItemSearched: "); |
| | | } |
| | | |
| | | // 获取权限 |
| | | private boolean checkPermission(String... per) { |
| | | if (EasyPermissions.hasPermissions(this, per)) { |
| | |
| | | //成功打开权限 |
| | | @Override |
| | | public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) { |
| | | init(savedInstanceState, mapView); |
| | | } |
| | | |
| | | //用户未同意权限 |
| | |
| | | EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); |
| | | } |
| | | |
| | | /*** |
| | | * 获取高德定位信息 |
| | | * @param aMapLocation |
| | | */ |
| | | @Override |
| | | public void onLocationChanged(AMapLocation aMapLocation) { |
| | | mAMapLocation = aMapLocation; |
| | | |
| | | } |
| | | |
| | | |
| | | /*** |
| | | * 定位初始化 |
| | | * @param onLocationChangedListener |
| | | */ |
| | | @Override |
| | | public void activate(OnLocationChangedListener onLocationChangedListener) { |
| | | |
| | | } |
| | | |
| | | public void initLocation() { |
| | | //初始化定位 |
| | | try { |
| | | mlocationClient = new AMapLocationClient(this); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //初始化定位参数 |
| | | mLocationOption = new AMapLocationClientOption(); |
| | | //设置定位回调监听 |
| | | mlocationClient.setLocationListener(this); |
| | | //设置为高精度定位模式 |
| | | mLocationOption.setNeedAddress(true); |
| | | mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); |
| | | //设置定位参数 |
| | | mlocationClient.setLocationOption(mLocationOption); |
| | | // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, |
| | | // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求 |
| | | // 在定位结束后,在合适的生命周期调用onDestroy()方法 |
| | | // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 |
| | | mlocationClient.startLocation();//启动定位 |
| | | } |
| | | |
| | | @Override |
| | | public void deactivate() { |
| | | if (mlocationClient != null) { |
| | | mlocationClient.stopLocation(); |
| | | mlocationClient.onDestroy(); |
| | | } |
| | | mlocationClient = null; |
| | | } |
| | | |
| | | @Override |
| | | public void onPointerCaptureChanged(boolean hasCapture) { |
| | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | if (mapView != null) |
| | | mapView.onDestroy(); |
| | | } |
| | | |
| | | public Polyline drawLine(LatLng start, LatLng end) { |
| | | List<LatLng> latLngs = new ArrayList<LatLng>(); |
| | | latLngs.add(start); |
| | | latLngs.add(end); |
| | | Polyline polyline = aMap.addPolyline(new PolylineOptions(). |
| | | addAll(latLngs).width(10).color(Color.argb(255, 1, 1, 1))); |
| | | return polyline; |
| | | } |
| | | |
| | | public Polyline drawLine(List<LatLng> latLngs) { |
| | | PolylineOptions options = new PolylineOptions().addAll(latLngs).width(30).color(Color.BLUE); |
| | | // //用一个数组来存放纹理 |
| | | // List<BitmapDescriptor> texturesList = new ArrayList<>(); |
| | | // texturesList.add(BitmapDescriptorFactory.fromResource(R.drawable.gps_point)); |
| | | // //指定某一段用某个纹理,对应texturesList的index即可, 三个点对应两段颜色 |
| | | // List<Integer> texIndexList = new ArrayList<>(); |
| | | // texIndexList.add(0);//对应上面的第0个纹理 |
| | | // //加入对应的颜色,使用setCustomTextureList 即表示使用多纹理; |
| | | // options.setCustomTextureList(texturesList); |
| | | // //设置纹理对应的Index |
| | | // options.setCustomTextureIndex(texIndexList); |
| | | Polyline polyline = aMap.addPolyline(options); |
| | | polyline.setDottedLine(true); |
| | | return polyline; |
| | | } |
| | | } |