OpenStreetMap开发文档

OpenStreetMap开发⽂档
前⾔
OpenStreetMap社区是⼀个由地图制作爱好者组成的社区,这些爱好者提供并维护世界各地关于道路、⼩道、咖啡馆、铁路车站等各种各样的数据。
OpenStreetMap开源项⽬可以让程序开发更加灵活,图源更加丰富,例如可以使⽤⾕歌地图,以解决国内⽆法使⽤⾕歌服务的尴尬。
国内户外导航软件,例如:、和都使⽤了OpenStreetMap。
Android版OpenStreetMap的github地址:
5.2地图缓存的是⽡⽚,5.4之后地图缓存到数据库
⼀、环境配置分丝辊
1、Gradle中添加依赖
compile 'org.osmdroid:osmdroid-android:5.2@aar'
2、权限配置
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>塑料水塔
提⽰:编译版本为23或以上版本,要注意动态获取读写存储空间权限,否则地图可能不显⽰
⼆、基础MapView使⽤
1、使⽤内置地图源
(1)、在布局⽂件中添加MapView控件,在代码中到控件并设置图源
<org.osmdroid.views.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
MapView mMapView= (MapView) findViewById(R.id.mapView);
mMapView.setTileSource(TileSourceFactory.CYCLEMAP);//(OCM等⾼)若不设置,则默认使⽤的是MAPNIK(OSM街道)
(2)、直接在代码中new MapView,然后设置图源,并将MapView添加到⽗布局中
MapView mMapView=new MapView(this);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
2、设置其他地图源
(1)、⾕歌图源
a、新建⼀个⾕歌图源类,继承OnlineTileSourceBase
public class GoogleMapsTileSource extends OnlineTileSourceBase {
/**
* @param aName                a human-friendly name for this tile source  ⾃定图源义名字,会在⼿机外部存储中新建以该名字命名的⽂件夹,⽡⽚存储在其中    * @param aZoomMinLevel        the minimum zoom level this tile source can provide  最⼩缩放级别
* @param aZoomMaxLevel        the maximum zoom level this tile source can provide  最⼤缩放级别
* @param aTileSizePixels      the tile size in pixels this tile source provides  ⽡⽚质量(256)
* @param aImageFilenameEnding the file name extension used when constructing the filename  ⽡⽚格式(jpg[有损压缩率⾼、不透明]、png[⽆损、透明    * @param aBaseUrl            the base url(s) of the tile server used when constructing the url to download the tiles  下载⽡⽚的链接(前缀)
*/
public GoogleMapsTileSource(String aName, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels, String aImageFilenameEnding, String[] aBase super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
}
@Override
public String getTileURLString(MapTile aTile) {
return getBaseUrl() + "&x=" + X() + "&y=" + Y() + "&z=" + ZoomLevel();
}
高精度室内定位}
b、new⼀个⾕歌图源对象,并设置MapView图源
String str1 = "le/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str2 = "le/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str3 = "le/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str4 = "le/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
GoogleMapsTileSource googleMapsTileSource = new GoogleMapsTileSource("GoogleNormal", 2, 19, 256, ".png", new String[]{str1, str2, str3, str4});
mMapView.setTileSource(googleMapsTileSource);
(2)、必应等图源,使⽤⽅法类似于⾕歌图源
3、让⽡⽚适应不同像素密度
默认地图显⽰的字体⼩,图⽚像素⾼,可设置以下代码,使地图适应不同像素密度,更美观
mMapView.setTilesScaledToDpi(true);
4、添加指南针
CompassOverlay mCompassOverlay = new CompassOverlay(MainActivity.this, new InternalCompassOrientationProvider(MainActivity.this), mMapView);
按此⽅法添加指南针之后,部分⼿机仍不显⽰指南针,原因未知
5、添加⽐例尺
ScaleBarOverlay mScaleBarOverlay = new ScaleBarOverlay(mMapView);自动折边机
添加上⾯代码后,⽐例尺显⽰在左上⾓,⽽且不美观,可以继续添加下⾯代码,使⽐例尺显⽰在左下⾓
mScaleBarOverlay.setAlignBottom(true);
mScaleBarOverlay.setLineWidth(1 * (getResources().getDisplayMetrics()).density);
mScaleBarOverlay.setMaxLength(0.85f);
6、设置地图中⼼
GeoPoint geopoint = new GeoPoint(39.986250, 116.400025);
MapController mMapController= (MapController) Controller();//获取MapView控制器
mMapController.setCenter(geopoint);//设置地图中⼼
7、其他设置
(1)、设置缩放界别
mMapController.setZoom(15);//设置缩放级别
(2)、设置缩放按钮可见
mMapView.setBuiltInZoomControls(true);//设置缩放按钮可见
(3)、设置多指触控可⽤
mMapView.setMultiTouchControls(true);//设置多指触控可⽤
(4)、关闭硬件加速
mMapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速(绘制轨迹时需要)
(5)、地图可旋转
RotationGestureOverlay mRotationGestureOverlay = new RotationGestureOverlay(this, mMapView);
mRotationGestureOverlay.setEnabled(true);
三、进阶使⽤
1、⾃定义⽡⽚缓存位置
默认会在外部存储中新建名为somdroid的⽂件夹,⽡⽚就存储在其中。
⾃定义缓存位置就是在外部存储中创建⼀个⽂件夹,然后设置为⽡⽚的缓存位置,放在MapView初始化之前 例如:
File dir = new ExternalStorageDirectory(), "AAA");//新建⽂件夹
if (ists()) {
File nomedia = new AbsoluteFile() + "/.nomedia");
if (!ists()) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
dir.mkdirs();
File file = AbsoluteFile();
try {
new File(file + "/.nomedia").createNewFile();
} catch (Exception ex) {
android.util.Log.e(IMapView.LOGTAG, "unable to create a nomedia file. downloaded tiles may be visible to the gallery.", ex);
}
}
OpenStreetMapTileProviderConstants.setCachePath(dir + "");//设置MapView的缓存路径
2、添加Marker
Marker marker = new Marker(mMapView);
marker.setIcon(getResources().getDrawable(R.mipmap.ic_launcher));//设置图标
marker.setPosition(geopoint);//设置位置
marker.setAnchor(0.5f, 0.5f);//设置偏移量
marker.setTitle("我是Titile");//设置标题
marker.setSubDescription("我是SubDescription");//设置说明
点击Marker之后会出现⽓泡,显⽰title和subDescription
捕虾机电路图
3、连线
PathOverlay pathOverlay = new PathOverlay(Color.BLUE, 10, this);
pathOverlay.addPoint(new GeoPoint(39.986250, 116.400025));
pathOverlay.addPoint(new GeoPoint(39.886250, 116.300025));
连线时务必关闭硬件加速,否则可能显⽰不出来连的线
4、离线地图下载
CacheManager cacheManager = new CacheManager(mMapView);//获取下载器
BoundingBoxE6 boundingBoxE6 = BoundingBox();//获取当前区域坛子鸡配方
int tileNum = cacheManager.possibleTilesInArea(boundingBoxE6, 8, 16);//计算当前区域8-16级的⽡⽚数量        cacheManager.downloadAreaAsync(this, boundingBoxE6, 8, 16, new CacheManager.CacheManagerCallback() {//下载@Override
public void onTaskComplete() {
//下载完成后的回调
}
});
下载时会有进度框,若点击进度框以外的区域会取消下载,若想修改逻辑可参考CacheManager,⾃定义⼀个CacheManage

本文发布于:2024-09-21 19:51:45,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/146149.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   地图   下载   添加   图源
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议