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

相关文章

Python利用GeoPandas打造一个交互式中国地图选择器

《Python利用GeoPandas打造一个交互式中国地图选择器》在数据分析和可视化领域,地图是展示地理信息的强大工具,被将使用Python、wxPython和GeoPandas构建的交互式中国地图行... 目录技术栈概览代码结构分析1. __init__ 方法:初始化与状态管理2. init_ui 方法:

SpringBoot加载profile全面解析

《SpringBoot加载profile全面解析》SpringBoot的Profile机制通过多配置文件和注解实现环境隔离,支持开发、测试、生产等不同环境的灵活配置切换,无需修改代码,关键点包括配置文... 目录题目详细答案什么是 Profile配置 Profile使用application-{profil

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

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

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

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

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