android osmdroid 加载离线地图map格式以及地图网格绘制

2023-12-30 05:50

本文主要是介绍android osmdroid 加载离线地图map格式以及地图网格绘制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

越来越深入了解osmdroid开源地图,越发现强大。继上次记载离线地图文章之后android osmdroid 加载常用离线地图格式(开源的在线地图)。再讲支持map地图格式的数据。还有怎么绘制地图的网格,绘制网络分为两种,一种是直接在地图上绘制,监听缩放等级。还有一种是利用Fragment加一层进行绘制。

osmdroid 仓库地址:http://jcenter.bintray.com/org/osmdroid/osmdroid-android/  这里面有示例apk,还有源码以及文档。

github官网:https://github.com/osmdroid/osmdroid

mapsforge相关库的仓库地址:http://jcenter.bintray.com/org/mapsforge/

map地图格式文件下载:http://ftp-stud.hs-esslingen.de/pub/Mirrors/download.mapsforge.org/maps/

1,先看实现的界面

网格实现



map地图格式



2,地图网格相关代码

直接加载版本,增加缩放监听即可

package com.osmdroid.sample;import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.FolderOverlay;
import org.osmdroid.views.overlay.gridlines.LatLonGridlineOverlay;public class DirectGridActivity extends AppCompatActivity implements View.OnClickListener {FolderOverlay activeLatLonGrid;private MapView mMapView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fragment);initView();}private void initView() {mMapView = (MapView)findViewById(R.id.mymapview);LatLonGridlineOverlay.setDefaults();mMapView.setMaxZoomLevel(20);mMapView.setMinZoomLevel(6);mMapView.getController().setZoom(12);mMapView.setTilesScaledToDpi(true);mMapView.getController().setCenter(new GeoPoint(23.12645, 113.365575));mMapView.setUseDataConnection(true);mMapView.setMultiTouchControls(true);// 触控放大缩小mMapView.getOverlayManager().getTilesOverlay().setEnabled(true);mMapView.setMapListener(mapListener);LatLonGridlineOverlay.fontSizeDp=16;LatLonGridlineOverlay.fontColor= Color.argb(255,0,255,0);LatLonGridlineOverlay.backgroundColor=Color.BLACK;LatLonGridlineOverlay.lineColor=LatLonGridlineOverlay.fontColor;updateGridlines();}MapListener mapListener = new MapListener() {@Overridepublic boolean onScroll(ScrollEvent scrollEvent) {updateGridlines();return false;}@Overridepublic boolean onZoom(ZoomEvent zoomEvent) {updateGridlines();return false;}};protected void updateGridlines(){if (mMapView==null)return; //happens during unit tests with rapid recycling of the fragmentif (activeLatLonGrid != null) {mMapView.getOverlayManager().remove(activeLatLonGrid);activeLatLonGrid.onDetach(mMapView);}LatLonGridlineOverlay.backgroundColor= Color.BLACK;LatLonGridlineOverlay.fontColor= Color.BLUE;LatLonGridlineOverlay.lineColor= Color.BLUE;activeLatLonGrid = LatLonGridlineOverlay.getLatLonGrid(this, mMapView);mMapView.getOverlays().add(activeLatLonGrid);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:break;}}
}

使用Fragment绘制格网

package com.osmdroid.sample;import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;import com.osmdroid.sample.util.SampleGridlines;import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;public class GridFragmentActivity extends AppCompatActivity  {private SampleGridlines mSampleGridlines = null;private MapView mapView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fragment);initView();}private void initView() {mapView = (MapView)findViewById(R.id.mymapview);//        mapView.setTileSource(TileSourceFactory.MAPNIK);mapView.setDrawingCacheEnabled(true);mapView.setMaxZoomLevel(20);mapView.setMinZoomLevel(6);mapView.getController().setZoom(12);mapView.getController().setCenter(new GeoPoint(23.12645, 113.365575));mapView.setUseDataConnection(true);mapView.setMultiTouchControls(true);// 触控放大缩小//是否显示地图数据源mapView.getOverlayManager().getTilesOverlay().setEnabled(false);FragmentManager fm = this.getSupportFragmentManager();if (fm.findFragmentByTag("SampleGridlines") == null) {mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);mSampleGridlines = new SampleGridlines();fm.beginTransaction().add(R.id.samples_container, mSampleGridlines, "SampleGridlines").commit();}}}

这个类也是osmdroid里面示例代码来的。我稍微修改了一下。

package com.osmdroid.sample.util;import android.graphics.Color;import com.osmdroid.sample.util.MapViewBaseSampleFragment;import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.FolderOverlay;
import org.osmdroid.views.overlay.gridlines.LatLonGridlineOverlay;/*** An example on how to use the lat/lon gridline overlay.** basically, listen for map motion/zoom events and remove the old overlay, then add the new one.* you can also override the color scheme and font sizes for the labels and lines*/
public class SampleGridlines extends MapViewBaseSampleFragment implements MapListener {FolderOverlay activeLatLonGrid;@Overridepublic String getSampleTitle() {return "Lat/Lon Gridlines";}@Overrideprotected void addOverlays() {super.addOverlays();LatLonGridlineOverlay.setDefaults();mMapView.setMaxZoomLevel(20);mMapView.setMinZoomLevel(6);mMapView.getController().setZoom(12);mMapView.getController().setCenter(new GeoPoint(23.12645, 113.365575));mMapView.setTilesScaledToDpi(true);mMapView.setMapListener(this);LatLonGridlineOverlay.fontSizeDp=16;LatLonGridlineOverlay.fontColor= Color.argb(255,0,255,0);LatLonGridlineOverlay.backgroundColor=Color.BLACK;LatLonGridlineOverlay.lineColor=LatLonGridlineOverlay.fontColor;updateGridlines();}@Overridepublic boolean onScroll(ScrollEvent scrollEvent) {updateGridlines();return false;}@Overridepublic boolean onZoom(ZoomEvent zoomEvent) {updateGridlines();return false;}protected void updateGridlines(){if (mMapView==null)return; //happens during unit tests with rapid recycling of the fragmentif (activeLatLonGrid != null) {mMapView.getOverlayManager().remove(activeLatLonGrid);activeLatLonGrid.onDetach(mMapView);}LatLonGridlineOverlay.backgroundColor= Color.BLACK;LatLonGridlineOverlay.fontColor= Color.BLUE;LatLonGridlineOverlay.lineColor= Color.BLUE;activeLatLonGrid = LatLonGridlineOverlay.getLatLonGrid(getActivity(), mMapView);mMapView.getOverlays().add(activeLatLonGrid);}@Overridepublic void onDestroyView(){if (activeLatLonGrid!=null)activeLatLonGrid.onDetach(mMapView);activeLatLonGrid=null;super.onDestroyView();}}

还有这个类的父类我就不贴出来。感兴趣下载示例代码以及apk对照。很多东西多看几眼就明白了。接下来说加载map格式地图

3,map与mapsforge

相关的库

    compile(name: 'osmdroid-android-5.6.3', ext: 'aar')compile files('libs/mapsforge-map-android-0.6.1.jar')compile files('libs/mapsforge-map-0.6.1.jar')compile files('libs/mapsforge-core-0.6.1.jar')compile files('libs/mapsforge-map-reader-0.6.1.jar')


还有这三个类


在osmdroid 示例代码也是可以找到的,或者jar包。

            //加载map地图格式if(fileName.contains(".map")){File[] mapfile = new File[1];mapfile[0] = exitFile;XmlRenderTheme theme = null;try {theme = new AssetsRenderTheme(context, "renderthemes/","rendertheme-v4.xml");AndroidGraphicFactory.createInstance(context.getApplication());}catch (Exception ex){ex.printStackTrace();return;}MapsForgeTileProvider forge = new MapsForgeTileProvider(new SimpleRegisterReceiver(context),MapsForgeTileSource.createFromFiles(mapfile,theme, "rendertheme-v4"),null);mapView.setTileProvider(forge);return;}
里面涉及到一个assets目录下建一个文件夹renderthemes,把rendertheme-v4.xml拷贝进去,相当于一个读取默认样式的。

最后嘱咐加上权限,联网以及读取目录的。

4,附上几张图,多了解一下





资源下载:后面上传之后再更新。

本博客源码下载:http://download.csdn.net/detail/qq_16064871/9763983



这篇关于android osmdroid 加载离线地图map格式以及地图网格绘制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/551895

相关文章

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Mysql常见的SQL语句格式及实用技巧

《Mysql常见的SQL语句格式及实用技巧》本文系统梳理MySQL常见SQL语句格式,涵盖数据库与表的创建、删除、修改、查询操作,以及记录增删改查和多表关联等高级查询,同时提供索引优化、事务处理、临时... 目录一、常用语法汇总二、示例1.数据库操作2.表操作3.记录操作 4.高级查询三、实用技巧一、常用语

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再