|
[[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レイアウトを追加する - 依存レイアウト
- 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 レイアウト構成 = 新しい DependentLayout.LayoutConfig();
- layoutConfig.width = DependentLayout.LayoutConfig.MATCH_PARENT;
- layoutConfig.height = DependentLayout.LayoutConfig.MATCH_PARENT;
-
- //MapView をインスタンス化する
- mapView = 新しい MapView(this);
- mMapContainerView.addComponent(mapView, 0, layoutConfig);
適切な権限を要求するconfig.json 構成ファイルに権限を追加する - ・・・・・・
-
- 「モジュール」 : {
- 「必要な権限」 : [
- { "name" : "ohos.permission.LOCATION" },
- { "name" : "ohos.permission.LOCATION_IN_BACKGROUND" },
- { "name" : "ohos.permission.ACCELEROMETER" },
- { "名前" : "ohos.permission.GET_NETWORK_INFO" },
- { "名前" : "ohos.permission.SET_NETWORK_INFO" },
- { "名前" : "ohos.permission.INTERNET" },
- { "name" : "ohos.permission.GYROSCOPE" },
- { "名前" : "ohos.permission.READ_USER_STORAGE" },
- { "名前" : "ohos.permission.WRITE_USER_STORAGE" }
- ],
-
- ・・・・・・
権限を動的に要求する機能です。 - パブリッククラス MainAbility は Ability を拡張します {
-
- プライベート文字列[] requestPermissions = {
- SystemPermission.WRITE_USER_STORAGE、
- SystemPermission.READ_USER_STORAGE、
- システム権限.LOCATION
- };
-
- @オーバーライド
- パブリックvoid onStart(インテント インテント) {
- super.onStart(インテント);
- super.setMainRoute(MainAbilitySlice.class.getName());
- PermissionsUtils.getInstance().requestPermissions(this, requestPermissions);
- }
-
- @オーバーライド
- パブリックvoid onRequestPermissionsFromUserResult( int requestCode, String[] 権限, int [] grantResults) {
- PermissionsUtils.getInstance().onRequestPermissionsResult(リクエストコード、権限、付与結果);
- }
- }
マルチタッチジェスチャを有効にする- // マルチタッチズームを有効にしてズームアウトと回転を行う
- MapView.setMultiTouchControls( true );
地図の中心点を設定する- mMapController = mapView.getController();
- //地図の中心点の位置を設定する
- mMapController.setCenter(新しいジオポイント(28.222567, 112.884651));
地図のズームレベルを設定する- mMapController = mapView.getController();
- //初期ズームレベルを設定する
- mMapController.setZoom(15.0);
オフラインマップの読み込み- //オフラインマップを読み込む
- ファイル file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getParentFile();
- 文字列 strFilepath = file.getPath() + "/osmdroid/" ;
-
- ファイル[] files = 新しいファイル(strFilepath).listFiles();
- if (files != null && files.length > 0) {
- ファイル exitFile = files[0];
- (exitFile.exists())の場合{
- 文字列ファイル名 = exitFile.getName();
- 文字列拡張子 =ファイル名.substring (ファイル名.lastIndexOf( "." ) + 1);
- ArchiveFileFactory.isFileExtensionRegistered(拡張子) の場合 {
- IRegisterReceiver レジスタレシーバー = 新しい SimpleRegisterReceiver(this);
- File[] offlineFiles = 新しい File[]{exitFile};
- OfflineTileProvider tileProvider = 新しい OfflineTileProvider(registerReceiver、offlineFiles);
- mapView.setTileProvider(tileProvider);
-
- IArchiveFile[] アーカイブ = tileProvider.getArchives();
- if (アーカイブの長さ > 0) {
- <String> tileSource = archives[0].getTileSources();を設定します。
- タイルソースが空の場合(!tileSource.isEmpty()){
- 文字列ソース = tileSource.iterator(). next ();
- mapView.setTileSource(FileBasedTileSource.getSource(ソース));
- mapView.setUseDataConnection( false );
- }
- }
- }
- }
- }
カバーを追加するコンパスを追加する - //コンパス
- InternalCompassOrientationProvider compassProvider = 新しい InternalCompassOrientationProvider(this);
- コンパスオーバーレイ mCompassOverlay = 新しいコンパスオーバーレイ(this、compassProvider、mapView);
- mCompassOverlay.enableCompass();
- mapView.getOverlays()。追加(mCompassOverlay);
位置情報を追加 - プライベート MapView mapView;
- プライベート MyLocationNewOverlay mLocationOverlay;
-
- @オーバーライド
- パブリックvoid onStart(インテント インテント) {
- super.onStart(インテント);
- super.setUIContent(ResourceTable.Layout_ability_main);
-
- if (isGrantedLocationPermission()) {
- マイロケーションオーバーレイマークを追加します。
- }それ以外{
- PermissionsUtils.getInstance().setRequestListener(権限 -> {
- if (permission.equals(SystemPermission.LOCATION)) {
- マイロケーションオーバーレイマークを追加します。
- }
- });
- }
- }
-
- @オーバーライド
- パブリックvoid onActive() {
- super.onActive();
- mapView.onResume();
- mLocationOverlay != null の場合{
- mLocationOverlay.enableMyLocation();
- }
- }
-
- @オーバーライド
- 保護されたvoid onInactive() {
- super.onInactive();
- mapView.onPause();
- mLocationOverlay != null の場合{
- mLocationOverlay.disableMyLocation();
- }
- }
-
- プライベートブール値isGrantedLocationPermission() {
- IBundleManager.PERMISSION_GRANTEDを返す
- == 呼び出しまたは自己権限を確認します(SystemPermission.LOCATION);
- }
-
- プライベートvoid addMyLocationOverlayMark() {
- //現在のデバイスに自動位置ポイントを追加するには、事前にデバイスの位置情報の許可が必要です。
- mLocationOverlay = 新しい MyLocationNewOverlay(mapView);
- ピクセルマップ personPixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_person);
- ピクセルマップの方向PixelMap = ResourceHelper.getPixmap(this, ResourceTable.Media_loc);
- mLocationOverlay.setDirectionArrow(personPixelMap, directionPixelMap);
- mapView.getOverlays()。追加(mLocationOverlay);
- }
地図の縮尺を追加 - //スケールバーを追加
- スケールバーオーバーレイ scaleBar = 新しい ScaleBarOverlay(mapView);
- スケールバーを中央に設定します( true );
- scaleBar.setAlignBottom( true ); // 下部に表示
- mapView.getOverlays()。 (スケールバー)を追加します。
マップの自由回転を追加 - //マップの自由回転
- 回転ジェスチャーオーバーレイ mRotationGestureOverlay = new RotationGestureOverlay(mapView);
- mRotationGestureOverlay.setEnabled( true );
- mapView.getOverlays()。追加(mRotationGestureOverlay);
ルート計画ルートポイントを追加する - //経路計画ポイント
- ポリライン ポリライン = new Polyline();
-
- //パス上のキー座標を追加する
- for ( int i = 0; i < size ; i++) {
- polyline.addPoint(新しい GeoPoint(緯度、経度));
- }
-
- //情報ボックスのタイトルを設定する
- polyline.setTitle( "タイトル" );
- //情報ボックスの内容を設定する
- ポリラインのサブディスクリプションを設定します(ポリラインの取得距離() + "" );
- //線幅を50に設定する
- ポリライン.getOutlinePaint().setStrokeWidth(20);
- // 線の色を赤に設定する
- ポリライン.getOutlinePaint().setColor(Color.BLUE);
- polyline.setInfoWindow(新しい BasicInfoWindow(ResourceTable.Layout_bonuspack_bubble、mapeView));
- mapView.getOverlayManager()。追加(ポリライン);
Giteeリポジトリアドレス https://gitee.com/talkwebyunchaung/osmdroid-ohos 詳細については、以下をご覧ください。 51CTOとHuaweiが共同で構築したHarmonyOSテクノロジーコミュニティ。 https://harmonyos..com |