DUICUO

HarmonyOSオープンソースマップ: OSMDroidマッププロジェクトをHarmonyOSに移植

[[414372]]

詳細については、以下をご覧ください。

51CTOとHuaweiが共同で構築したHarmonyOSテクノロジーコミュニティ。

https://harmonyos..com

OSMDroid-ohos

導入

OSMDroid-ohosは、GoogleのオープンソースマッププロジェクトOSMDroidをHarmonyOSシステムに移植した簡素化版であり、HarmonyOS上でのマップアプリケーション開発を可能にします。現在、OSMDroid-ohosマップは、中国の開発状況に基づき、Gaodeのタイルマップ表示をデフォルトとしています。

OSMDroid-ohos Maps は、基本的なタイル マップ表示、マップ コンパス オーバーレイ、マップ位置座標オーバーレイを提供し、1 本指ジェスチャによるマップのドラッグ、1 本指ジェスチャによるダブルタップによるズームイン、2 本指ジェスチャによるマップのズーム、2 本指ジェスチャによるマップの回転などの機能を提供します。



MapViewの使用法

XMLレイアウトを追加する

  1. 依存レイアウト
  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:horizo​​ntal_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 レイアウト構成 = 新しい DependentLayout.LayoutConfig();
  3. layoutConfig.width = DependentLayout.LayoutConfig.MATCH_PARENT;
  4. layoutConfig.height = DependentLayout.LayoutConfig.MATCH_PARENT;
  5.  
  6. //MapView をインスタンス化する
  7. mapView = 新しい MapView(this);
  8. mMapContainerView.addComponent(mapView, 0, layoutConfig);

適切な権限を要求する

config.json 構成ファイルに権限を追加する

  1. ・・・・・・
  2.  
  3. 「モジュール」 : {
  4. 「必要な権限」 : [
  5. { "name" : "ohos.permission.LOCATION" },
  6. { "name" : "ohos.permission.LOCATION_IN_BACKGROUND" },
  7. { "name" : "ohos.permission.ACCELEROMETER" },
  8. { "名前" : "ohos.permission.GET_NETWORK_INFO" },
  9. { "名前" : "ohos.permission.SET_NETWORK_INFO" },
  10. { "名前" : "ohos.permission.INTERNET" },
  11. { "name" : "ohos.permission.GYROSCOPE" },
  12. { "名前" : "ohos.permission.READ_USER_STORAGE" },
  13. { "名前" : "ohos.permission.WRITE_USER_STORAGE" }
  14. ],
  15.      
  16. ・・・・・・

権限を動的に要求する機能です。

  1. パブリッククラス MainAbility は Ability を拡張します {
  2.  
  3. プライベート文字列[] requestPermissions = {
  4. SystemPermission.WRITE_USER_STORAGE、
  5. SystemPermission.READ_USER_STORAGE、
  6. システム権限.LOCATION
  7. };
  8.  
  9. @オーバーライド
  10. パブリックvoid onStart(インテント インテント) {
  11. super.onStart(インテント);
  12. super.setMainRoute(MainAbilitySlice.class.getName());
  13. PermissionsUtils.getInstance().requestPermissions(this, requestPermissions);
  14. }
  15.  
  16. @オーバーライド
  17. パブリックvoid onRequestPermissionsFromUserResult( int requestCode, String[] 権限, int [] grantResults) {
  18. PermissionsUtils.getInstance().onRequestPermissionsResult(リクエストコード、権限、付与結果);
  19. }
  20. }

マルチタッチジェスチャを有効にする

  1. // マルチタッチズームを有効にしてズームアウトと回転を行う
  2. MapView.setMultiTouchControls( true );

地図の中心点を設定する

  1. mMapController = mapView.getController();
  2. //地図の中心点の位置を設定する
  3. mMapController.setCenter(新しいジオポイント(28.222567, 112.884651));

地図のズームレベルを設定する

  1. mMapController = mapView.getController();
  2. //初期ズームレベルを設定する
  3. mMapController.setZoom(15.0);

オフラインマップの読み込み

  1. //オフラインマップを読み込む
  2. ファイル file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getParentFile();
  3. 文字列 strFilepath = file.getPath() + "/osmdroid/" ;
  4.  
  5. ファイル[] files = 新しいファイル(strFilepath).listFiles();
  6. if (files != null && files.length > 0) {
  7. ファイル exitFile = files[0];
  8. (exitFile.exists())の場合{
  9. 文字列ファイル名 = exitFile.getName();
  10. 文字列拡張子 =ファイル名.substring (ファイル名.lastIndexOf( "." ) + 1);
  11. ArchiveFileFactory.isFileExtensionRegistered(拡張子) の場合 {
  12. IRegisterReceiver レジスタレシーバー = 新しい SimpleRegisterReceiver(this);
  13. File[] offlineFiles = 新しい File[]{exitFile};
  14. OfflineTileProvider tileProvider = 新しい OfflineTileProvider(registerReceiver、offlineFiles);
  15. mapView.setTileProvider(tileProvider);
  16.  
  17. IArchiveFile[] アーカイブ = tileProvider.getArchives();
  18. if (アーカイブの長さ > 0) {
  19. <String> tileSource = archives[0].getTileSources();を設定します
  20. タイルソースが空の場合(!tileSource.isEmpty()){
  21. 文字列ソース = tileSource.iterator(). next ();
  22. mapView.setTileSource(FileBasedTileSource.getSource(ソース));
  23. mapView.setUseDataConnection( false );
  24. }
  25. }
  26. }
  27. }
  28. }

カバーを追加する

コンパスを追加する

  1. //コンパス
  2. InternalCompassOrientationProvider compassProvider = 新しい InternalCompassOrientationProvider(this);
  3. コンパスオーバーレイ mCompassOverlay = 新しいコンパスオーバーレイ(this、compassProvider、mapView);
  4. mCompassOverlay.enableCompass();
  5. mapView.getOverlays()。追加(mCompassOverlay);

位置情報を追加

  1. プライベート MapView mapView;
  2. プライベート MyLocationNewOverlay mLocationOverlay;
  3.  
  4. @オーバーライド
  5. パブリックvoid onStart(インテント インテント) {
  6. super.onStart(インテント);
  7. super.setUIContent(ResourceTable.Layout_ability_main);
  8.  
  9. if (isGrantedLocationPermission()) {
  10. マイロケーションオーバーレイマークを追加します。
  11. }それ以外{
  12. PermissionsUtils.getInstance().setRequestListener(権限 -> {
  13. if (permission.equals(SystemPermission.LOCATION)) {
  14. マイロケーションオーバーレイマークを追加します。
  15. }
  16. });
  17. }
  18. }
  19.  
  20. @オーバーライド
  21. パブリックvoid onActive() {
  22. super.onActive();
  23. mapView.onResume();
  24. mLocationOverlay != null の場合{
  25. mLocationOverlay.enableMyLocation();
  26. }
  27. }
  28.  
  29. @オーバーライド
  30. 保護されたvoid onInactive() {
  31. super.onInactive();
  32. mapView.onPause();
  33. mLocationOverlay != null の場合{
  34. mLocationOverlay.disableMyLocation();
  35. }
  36. }
  37.      
  38. プライベートブール値isGrantedLocationPermission() {
  39. IBundleManager.PERMISSION_GRANTEDを返す
  40. == 呼び出しまたは自己権限を確認します(SystemPermission.LOCATION);
  41. }
  42.  
  43. プライベートvoid addMyLocationOverlayMark() {
  44. //現在のデバイスに自動位置ポイントを追加するには、事前にデバイスの位置情報の許可が必要です。
  45. mLocationOverlay = 新しい MyLocationNewOverlay(mapView);
  46. ピクセルマップ personPixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_person);
  47. ピクセルマップの方向PixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_loc);
  48. mLocationOverlay.setDirectionArrow(personPixelMap, directionPixelMap);
  49. mapView.getOverlays()。追加(mLocationOverlay);
  50. }

地図の縮尺を追加

  1. //スケールバーを追加
  2. スケールバーオーバーレイ scaleBar = 新しい ScaleBarOverlay(mapView);
  3. スケールバーを中央に設定します( true );
  4. scaleBar.setAlignBottom( true ); // 下部に表示
  5. mapView.getOverlays()。 (スケールバー)を追加します

マップの自由回転を追加

  1. //マップの自由回転
  2. 回転ジェスチャーオーバーレイ mRotationGestureOverlay = new RotationGestureOverlay(mapView);
  3. mRotationGestureOverlay.setEnabled( true );
  4. mapView.getOverlays()。追加(mRotationGestureOverlay);

ルート計画ルートポイントを追加する

  1. //経路計画ポイント
  2. ポリライン ポリライン = new Polyline();
  3.  
  4. //パス上のキー座標を追加する
  5. for ( int i = 0; i < size ; i++) {
  6. polyline.addPoint(新しい GeoPoint(緯度、経度));
  7. }
  8.  
  9. //情報ボックスのタイトルを設定する
  10. polyline.setTitle( "タイトル" );
  11. //情報ボックスの内容を設定する
  12. ポリラインのサブディスクリプションを設定します(ポリラインの取得距離() + "" );
  13. //線幅を50に設定する
  14. ポリライン.getOutlinePaint().setStrokeWidth(20);
  15. // 線の色を赤に設定する
  16. ポリライン.getOutlinePaint().setColor(Color.BLUE);
  17. polyline.setInfoWindow(新しい BasicInfoWindow(ResourceTable.Layout_bonuspack_bubble、mapeView));
  18. mapView.getOverlayManager()。追加(ポリライン);

Giteeリポジトリアドレス

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

詳細については、以下をご覧ください。

51CTOとHuaweiが共同で構築したHarmonyOSテクノロジーコミュニティ。

https://harmonyos..com