鴻蒙開源地圖,OSMDroid地圖項目移植到鴻蒙上
OSMDroid-ohos
介紹
OSMDroid-ohos是Google開源地圖項目OSMDroid移植到HarmonyOS系統上的精簡版,可以實現在HarmonyOS系統上做地圖應用開發。當前OSMDroid-ohos地圖根據國內開發情況,默認移植了高德的瓦片地圖顯示。
OSMDroid-ohos地圖提供基本瓦片地圖顯示,地圖指南針覆蓋物,地圖定位坐標覆蓋物,提供單指手勢拖拽地圖功能,單指手勢雙擊放大地圖功能,雙指手勢縮放地圖功能,雙指手勢旋轉地圖功能。


MapView使用
xml布局添加
- <DependentLayout
- ohos:id="$+id:osm_map_container"
- ohos:height="match_content"
- ohos:width="match_parent">
- <com.talkweb.osmharmony.views.MapView
- ohos:id="$+id:osm_map_view"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:horizontal_center="true"/>
- ·················
- mMapContainerView = (DependentLayout) findComponentById(ResourceTable.Id_osm_map_container);
- mapView = (MapView) findComponentById(ResourceTable.Id_osm_map_view);
java代碼添加
- mMapContainerView = (DependentLayout) findComponentById(ResourceTable.Id_osm_map_container);
- DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig();
- layoutConfig.width = DependentLayout.LayoutConfig.MATCH_PARENT;
- layoutConfig.height = DependentLayout.LayoutConfig.MATCH_PARENT;
- //實例化MapView
- mapView = new MapView(this);
- mMapContainerView.addComponent(mapView, 0, layoutConfig);
請求相應權限
config.json配置文件添加權限
- ······
- "module": {
- "reqPermissions": [
- {"name": "ohos.permission.LOCATION"},
- {"name": "ohos.permission.LOCATION_IN_BACKGROUND"},
- {"name": "ohos.permission.ACCELEROMETER"},
- {"name": "ohos.permission.GET_NETWORK_INFO"},
- {"name": "ohos.permission.SET_NETWORK_INFO"},
- {"name": "ohos.permission.INTERNET"},
- {"name": "ohos.permission.GYROSCOPE"},
- {"name": "ohos.permission.READ_USER_STORAGE"},
- {"name": "ohos.permission.WRITE_USER_STORAGE"}
- ],
- ······
Ability動態請求權限
- public class MainAbility extends Ability {
- private String[] requestPermissions = {
- SystemPermission.WRITE_USER_STORAGE,
- SystemPermission.READ_USER_STORAGE,
- SystemPermission.LOCATION
- };
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setMainRoute(MainAbilitySlice.class.getName());
- PermissionsUtils.getInstance().requestPermissions(this, requestPermissions);
- }
- @Override
- public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
- PermissionsUtils.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
- }
- }
啟動多點觸控手勢
- // 啟動多點觸控放大縮小旋轉
- MapView.setMultiTouchControls(true);
設置地圖中心點
- mMapController = mapView.getController();
- //設置地圖中心點位置
- mMapController.setCenter(new GeoPoint(28.222567, 112.884651));
設置地圖縮放級別
- mMapController = mapView.getController();
- //設置初始化縮放級別
- mMapController.setZoom(15.0);
離線地圖加載
- //加載離線地圖
- File file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getParentFile();
- String strFilepath = file.getPath() + "/osmdroid/";
- File[] files = new File(strFilepath).listFiles();
- if (files != null && files.length > 0) {
- File exitFile = files[0];
- if (exitFile.exists()) {
- String filename = exitFile.getName();
- String extension = filename.substring(filename.lastIndexOf(".") + 1);
- if (ArchiveFileFactory.isFileExtensionRegistered(extension)) {
- IRegisterReceiver registerReceiver = new SimpleRegisterReceiver(this);
- File[] offlineFiles = new File[]{exitFile};
- OfflineTileProvider tileProvider = new OfflineTileProvider(registerReceiver, offlineFiles);
- mapView.setTileProvider(tileProvider);
- IArchiveFile[] archives = tileProvider.getArchives();
- if (archives.length > 0) {
- Set<String> tileSource = archives[0].getTileSources();
- if (!tileSource.isEmpty()) {
- String source = tileSource.iterator().next();
- mapView.setTileSource(FileBasedTileSource.getSource(source));
- mapView.setUseDataConnection(false);
- }
- }
- }
- }
- }
添加覆蓋物
添加指南針
- //指南針
- InternalCompassOrientationProvider compassProvider = new InternalCompassOrientationProvider(this);
- CompassOverlay mCompassOverlay = new CompassOverlay(this, compassProvider, mapView);
- mCompassOverlay.enableCompass();
- mapView.getOverlays().add(mCompassOverlay);
添加我的定位點
- private MapView mapView;
- private MyLocationNewOverlay mLocationOverlay;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- if (isGrantedLocationPermission()) {
- addMyLocationOverlayMark();
- } else {
- PermissionsUtils.getInstance().setRequestListener(permission -> {
- if (permission.equals(SystemPermission.LOCATION)) {
- addMyLocationOverlayMark();
- }
- });
- }
- }
- @Override
- public void onActive() {
- super.onActive();
- mapView.onResume();
- if (mLocationOverlay != null) {
- mLocationOverlay.enableMyLocation();
- }
- }
- @Override
- protected void onInactive() {
- super.onInactive();
- mapView.onPause();
- if (mLocationOverlay != null) {
- mLocationOverlay.disableMyLocation();
- }
- }
- private boolean isGrantedLocationPermission() {
- return IBundleManager.PERMISSION_GRANTED
- == verifyCallingOrSelfPermission(SystemPermission.LOCATION);
- }
- private void addMyLocationOverlayMark() {
- //添加當前設備自動定位點,需要先具有設備位置權限
- mLocationOverlay = new MyLocationNewOverlay(mapView);
- PixelMap personPixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_person);
- PixelMap directionPixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_loc);
- mLocationOverlay.setDirectionArrow(personPixelMap, directionPixelMap);
- mapView.getOverlays().add(mLocationOverlay);
- }
添加地圖比例尺
- //添加比例尺
- ScaleBarOverlay scaleBar = new ScaleBarOverlay(mapView);
- scaleBar.setCentred(true);
- scaleBar.setAlignBottom(true); //底部顯示
- mapView.getOverlays().add(scaleBar);
添加地圖自由旋轉
- //地圖自由旋轉
- RotationGestureOverlay mRotationGestureOverlay = new RotationGestureOverlay(mapView);
- mRotationGestureOverlay.setEnabled(true);
- mapView.getOverlays().add(mRotationGestureOverlay);
添加路徑規劃線路點
- //路徑規劃點
- Polyline polyline = new Polyline();
- //添加路徑上的關鍵坐標點
- for(int i = 0; i < size; i++) {
- polyline.addPoint(new GeoPoint(latitude, longitude));
- }
- //設置信息框標題
- polyline.setTitle("title");
- //設置信息框內容
- polyline.setSubDescription(polyline.getDistance() + "");
- //設置線寬度為50
- polyline.getOutlinePaint().setStrokeWidth(20);
- //設置線的顏色為紅色
- polyline.getOutlinePaint().setColor(Color.BLUE);
- polyline.setInfoWindow(new BasicInfoWindow(ResourceTable.Layout_bonuspack_bubble, mapeView));
- mapView.getOverlayManager().add(polyline);
碼云倉庫地址
https://gitee.com/talkwebyunchaung/osmdroid-ohos