鸿蒙开源地图,OSMDroid地图项目移植到鸿蒙上

开源
OSMDroid-ohos是Google开源地图项目OSMDroid移植到HarmonyOS系统上的精简版,可以实现在HarmonyOS系统上做地图应用开发。当前OSMDroid-ohos地图根据国内开发情况,默认移植了高德的瓦片地图显示。

[[414372]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

OSMDroid-ohos

介绍

OSMDroid-ohos是Google开源地图项目OSMDroid移植到HarmonyOS系统上的精简版,可以实现在HarmonyOS系统上做地图应用开发。当前OSMDroid-ohos地图根据国内开发情况,默认移植了高德的瓦片地图显示。

OSMDroid-ohos地图提供基本瓦片地图显示,地图指南针覆盖物,地图定位坐标覆盖物,提供单指手势拖拽地图功能,单指手势双击放大地图功能,双指手势缩放地图功能,双指手势旋转地图功能。

鸿蒙开源地图,OSMDroid地图项目移植到鸿蒙上-鸿蒙HarmonyOS技术社区
鸿蒙开源地图,OSMDroid地图项目移植到鸿蒙上-鸿蒙HarmonyOS技术社区

MapView使用

xml布局添加

  1. <DependentLayout 
  2.       ohos:id="$+id:osm_map_container" 
  3.       ohos:height="match_content" 
  4.       ohos:width="match_parent"
  5.  
  6.       <com.talkweb.osmharmony.views.MapView 
  7.           ohos:id="$+id:osm_map_view" 
  8.           ohos:height="match_parent" 
  9.           ohos:width="match_parent" 
  10.           ohos:horizontal_center="true"/> 
  11.        
  12.       ················· 
  1. mMapContainerView = (DependentLayout) findComponentById(ResourceTable.Id_osm_map_container); 
  2. mapView = (MapView) findComponentById(ResourceTable.Id_osm_map_view); 

java代码添加

  1. mMapContainerView = (DependentLayout) findComponentById(ResourceTable.Id_osm_map_container); 
  2. DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(); 
  3. layoutConfig.width = DependentLayout.LayoutConfig.MATCH_PARENT; 
  4. layoutConfig.height = DependentLayout.LayoutConfig.MATCH_PARENT; 
  5.  
  6. //实例化MapView 
  7. mapView = new MapView(this); 
  8. mMapContainerView.addComponent(mapView, 0, layoutConfig); 

请求相应权限

config.json配置文件添加权限

  1. ······  
  2.  
  3. "module": { 
  4.     "reqPermissions": [ 
  5.       {"name""ohos.permission.LOCATION"}, 
  6.       {"name""ohos.permission.LOCATION_IN_BACKGROUND"}, 
  7.       {"name""ohos.permission.ACCELEROMETER"}, 
  8.       {"name""ohos.permission.GET_NETWORK_INFO"}, 
  9.       {"name""ohos.permission.SET_NETWORK_INFO"}, 
  10.       {"name""ohos.permission.INTERNET"}, 
  11.       {"name""ohos.permission.GYROSCOPE"}, 
  12.       {"name""ohos.permission.READ_USER_STORAGE"}, 
  13.       {"name""ohos.permission.WRITE_USER_STORAGE"
  14.     ], 
  15.      
  16. ······ 

Ability动态请求权限

  1. public class MainAbility extends Ability { 
  2.  
  3.     private String[] requestPermissions = { 
  4.             SystemPermission.WRITE_USER_STORAGE,  
  5.             SystemPermission.READ_USER_STORAGE,  
  6.             SystemPermission.LOCATION 
  7.     }; 
  8.  
  9.     @Override 
  10.     public void onStart(Intent intent) { 
  11.         super.onStart(intent); 
  12.         super.setMainRoute(MainAbilitySlice.class.getName()); 
  13.         PermissionsUtils.getInstance().requestPermissions(this, requestPermissions); 
  14.     } 
  15.  
  16.     @Override 
  17.     public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { 
  18.         PermissionsUtils.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults); 
  19.     } 

启动多点触控手势

  1. // 启动多点触控放大缩小旋转 
  2. MapView.setMultiTouchControls(true); 

设置地图中心点

  1. mMapController = mapView.getController(); 
  2. //设置地图中心点位置 
  3. mMapController.setCenter(new GeoPoint(28.222567, 112.884651)); 

设置地图缩放级别

  1. mMapController = mapView.getController(); 
  2. //设置初始化缩放级别 
  3. mMapController.setZoom(15.0); 

离线地图加载

  1. //加载离线地图 
  2. File file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getParentFile(); 
  3. String strFilepath = file.getPath() + "/osmdroid/"
  4.  
  5. File[] files = new File(strFilepath).listFiles(); 
  6. if (files != null && files.length > 0) { 
  7.     File exitFile = files[0]; 
  8.     if (exitFile.exists()) { 
  9.         String filename = exitFile.getName(); 
  10.         String extension = filename.substring(filename.lastIndexOf(".") + 1); 
  11.         if (ArchiveFileFactory.isFileExtensionRegistered(extension)) { 
  12.             IRegisterReceiver registerReceiver = new SimpleRegisterReceiver(this); 
  13.             File[] offlineFiles = new File[]{exitFile}; 
  14.             OfflineTileProvider tileProvider = new OfflineTileProvider(registerReceiver, offlineFiles); 
  15.             mapView.setTileProvider(tileProvider); 
  16.  
  17.             IArchiveFile[] archives = tileProvider.getArchives(); 
  18.             if (archives.length > 0) { 
  19.                 Set<String> tileSource = archives[0].getTileSources(); 
  20.                 if (!tileSource.isEmpty()) { 
  21.                     String source = tileSource.iterator().next(); 
  22.                     mapView.setTileSource(FileBasedTileSource.getSource(source)); 
  23.                     mapView.setUseDataConnection(false); 
  24.                 } 
  25.             } 
  26.         } 
  27.     } 

添加覆盖物

添加指南针

  1. //指南针 
  2. InternalCompassOrientationProvider compassProvider = new InternalCompassOrientationProvider(this); 
  3. CompassOverlay mCompassOverlay = new CompassOverlay(this, compassProvider, mapView); 
  4. mCompassOverlay.enableCompass(); 
  5. mapView.getOverlays().add(mCompassOverlay); 

添加我的定位点

  1. private MapView mapView; 
  2. private MyLocationNewOverlay mLocationOverlay; 
  3.  
  4. @Override 
  5. public void onStart(Intent intent) { 
  6.     super.onStart(intent); 
  7.     super.setUIContent(ResourceTable.Layout_ability_main); 
  8.  
  9.     if (isGrantedLocationPermission()) { 
  10.         addMyLocationOverlayMark(); 
  11.     } else { 
  12.         PermissionsUtils.getInstance().setRequestListener(permission -> { 
  13.             if (permission.equals(SystemPermission.LOCATION)) { 
  14.                 addMyLocationOverlayMark(); 
  15.             } 
  16.         }); 
  17.     } 
  18.  
  19. @Override 
  20. public void onActive() { 
  21.     super.onActive(); 
  22.     mapView.onResume(); 
  23.     if (mLocationOverlay != null) { 
  24.         mLocationOverlay.enableMyLocation(); 
  25.     } 
  26.  
  27. @Override 
  28. protected void onInactive() { 
  29.     super.onInactive(); 
  30.     mapView.onPause(); 
  31.     if (mLocationOverlay != null) { 
  32.         mLocationOverlay.disableMyLocation(); 
  33.     } 
  34.      
  35. private boolean isGrantedLocationPermission() { 
  36.     return IBundleManager.PERMISSION_GRANTED  
  37.         == verifyCallingOrSelfPermission(SystemPermission.LOCATION); 
  38.  
  39. private void addMyLocationOverlayMark() { 
  40.     //添加当前设备自动定位点,需要先具有设备位置权限 
  41.     mLocationOverlay = new MyLocationNewOverlay(mapView); 
  42.     PixelMap personPixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_person); 
  43.     PixelMap directionPixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_loc); 
  44.     mLocationOverlay.setDirectionArrow(personPixelMap, directionPixelMap); 
  45.     mapView.getOverlays().add(mLocationOverlay); 
  46. }    

添加地图比例尺

  1. //添加比例尺 
  2. ScaleBarOverlay scaleBar = new ScaleBarOverlay(mapView); 
  3. scaleBar.setCentred(true); 
  4. scaleBar.setAlignBottom(true); //底部显示 
  5. mapView.getOverlays().add(scaleBar); 

添加地图自由旋转

  1. //地图自由旋转 
  2. RotationGestureOverlay mRotationGestureOverlay = new RotationGestureOverlay(mapView); 
  3. mRotationGestureOverlay.setEnabled(true); 
  4. mapView.getOverlays().add(mRotationGestureOverlay); 

添加路径规划线路点

  1. //路径规划点 
  2. Polyline polyline = new Polyline(); 
  3.  
  4. //添加路径上的关键坐标点 
  5. for(int i = 0; i < size; i++) { 
  6.     polyline.addPoint(new GeoPoint(latitude, longitude)); 
  7.  
  8. //设置信息框标题 
  9. polyline.setTitle("title"); 
  10. //设置信息框内容 
  11. polyline.setSubDescription(polyline.getDistance() + ""); 
  12. //设置线宽度为50 
  13. polyline.getOutlinePaint().setStrokeWidth(20); 
  14. //设置线的颜色为红色 
  15. polyline.getOutlinePaint().setColor(Color.BLUE); 
  16. polyline.setInfoWindow(new BasicInfoWindow(ResourceTable.Layout_bonuspack_bubble, mapeView)); 
  17. mapView.getOverlayManager().add(polyline); 

码云仓库地址

https://gitee.com/talkwebyunchaung/osmdroid-ohos

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-12-23 11:36:23

鸿蒙HarmonyOS应用程序开发

2020-12-07 12:34:33

开发板鸿蒙hello world

2020-11-26 11:56:25

freeModbusT

2024-01-18 10:10:27

2021-03-17 15:49:03

鸿蒙HarmonyOS应用

2009-04-23 17:37:05

LinuxRed Hat开源

2021-01-25 16:39:08

鸿蒙HarmonyOS添加单板

2021-01-26 13:50:43

鸿蒙HarmonyOS应用开发

2021-06-15 14:33:00

高德百度腾讯

2013-05-16 14:31:49

GoogleGoogle Maps

2010-08-18 09:43:55

2021-03-18 08:11:18

PythonDash工具

2021-02-22 14:55:41

鸿蒙HarmonyOS应用开发

2020-12-29 11:51:16

鸿蒙HarmonyOS华为手机

2012-09-18 13:13:17

2012-11-27 10:07:24

eWeek诺基亚Here地图

2011-09-29 11:00:54

百度地图API
点赞
收藏

51CTO技术栈公众号