鸿蒙开源地图,OSMDroid地图项目移植到鸿蒙上 原创 精华

拓维信息
发布于 2021-7-27 09:07
浏览
9收藏

OSMDroid-ohos

介绍

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

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

鸿蒙开源地图,OSMDroid地图项目移植到鸿蒙上-鸿蒙开发者社区

鸿蒙开源地图,OSMDroid地图项目移植到鸿蒙上-鸿蒙开发者社区

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

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2021-7-27 10:06:49修改
12
收藏 9
回复
举报
6条回复
按时间正序
/
按时间倒序
XFJingGG
XFJingGG

非常奈斯

回复
2021-7-27 15:48:05
学徒王小剑
学徒王小剑

gooood

1
回复
2021-7-27 16:38:54
XY道衍
XY道衍

gooood

1
回复
2021-7-27 17:18:35
Whyalone
Whyalone

目前有没有国内地图的接入接口?

华为地图、腾讯地图、百度地图、高德地图这类的地图接入APP和安卓的接入有没有本质上的不同?或者接口上的变动?

回复
2021-7-27 17:32:00
拓维信息Abin
拓维信息Abin

OSMDroid-ohos只是一个地图加载框架,当前已经默认实现了高德地图的瓦片加载;

只要你拥有华为地图、腾讯地图、百度地图和高德地图个地图商的瓦片http请求接口,以及对应地图经纬度坐标转换接口,都可以加入到OSMDroid-ohos中。

比如高德地图瓦片加入OSMDroid-ohos中:

public static final OnlineTileSourceBase GAO_DE_MAP = new XYTileSource("AutoNavi-Vector",
            3, 18, 256, ".png", new String[]{
            "https://wprd04.is.autonavi.com/appmaptile?",
            "https://wprd03.is.autonavi.com/appmaptile?",
            "https://wprd02.is.autonavi.com/appmaptile?",
            "https://wprd01.is.autonavi.com/appmaptile?",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {
            return getBaseUrl() + "x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z="
                    + MapTileIndex.getZoom(pMapTileIndex) + "&lang=zh_cn&size=1&scl=1&style=7&ltype=7";
        }
    };

//MapView添加高德地图瓦片源
mMapView.setTileSource(TileSourceFactory.GAO_DE_MAP);
1
回复
2021-7-28 10:05:51
安之__
安之__

你好,我想问一下,OSMDroid-ohos如何接入到鸿蒙项目中和调用呢

回复
2024-1-13 21:17:11
回复
    相关推荐