有没有用 R 和 openstreetmap wms做地图的

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[] aBaseUrl) {
super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
public String getTileURLString(MapTile aTile) {
return getBaseUrl() + "&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
b、new一个谷歌图源对象,并设置MapView图源
String str1 = "/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str2 = "/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str3 = "/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str4 = "/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);
mMapView.getOverlays().add(mCompassOverlay);
mCompassOverlay.enableCompass();
按此方法添加指南针之后,部分手机仍不显示指南针,原因未知
5、添加比例尺
ScaleBarOverlay mScaleBarOverlay = new ScaleBarOverlay(mMapView);
mMapView.getOverlays().add(mScaleBarOverlay);
添加上面代码后,比例尺显示在左上角,而且不美观,可以继续添加下面代码,使比例尺显示在左下角
mScaleBarOverlay.setAlignBottom(true);
mScaleBarOverlay.setLineWidth(1 * (getResources().getDisplayMetrics()).density);
mScaleBarOverlay.setMaxLength(0.85f);
6、设置地图中心
GeoPoint geopoint = new GeoPoint(39.6.400025);
MapController mMapController= (MapController) mMapView.getController();//获取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);
mMapView.getOverlays().add(mRotationGestureOverlay);
三、进阶使用
1、自定义瓦片缓存位置
默认会在外部存储中新建名为somdroid的文件夹,瓦片就存储在其中。
自定义缓存位置就是在外部存储中创建一个文件夹,然后设置为瓦片的缓存位置,放在MapView初始化之前例如:
File dir = new File(Environment.getExternalStorageDirectory(), "AAA");//新建文件夹
if (dir.exists()) {
File nomedia = new File(dir.getAbsoluteFile() + "/.nomedia");
if (!nomedia.exists()) {
nomedia.createNewFile();
} catch (IOException e) {
e.printStackTrace();
dir.mkdirs();
File file = dir.getAbsoluteFile();
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");//设置说明
mMapView.getOverlays().add(marker);//添加marker到MapView
点击Marker之后会出现气泡,显示title和subDescription
PathOverlay pathOverlay = new PathOverlay(Color.BLUE, 10, this);
pathOverlay.addPoint(new GeoPoint(39.6.400025));
pathOverlay.addPoint(new GeoPoint(39.6.300025));
mMapView.getOverlays().add(pathOverlay);
连线时务必关闭硬件加速,否则可能显示不出来连的线
4、离线地图下载
CacheManager cacheManager = new CacheManager(mMapView);//获取下载器
BoundingBoxE6 boundingBoxE6 = mMapView.getBoundingBox();//获取当前区域
int tileNum = cacheManager.possibleTilesInArea(boundingBoxE6, 8, 16);//计算当前区域8-16级的瓦片数量
cacheManager.downloadAreaAsync(this, boundingBoxE6, 8, 16, new CacheManager.CacheManagerCallback() {//下载
public void onTaskComplete() {
//下载完成后的回调
下载时会有进度框,若点击进度框以外的区域会取消下载,若想修改逻辑可参考CacheManager,自定义一个CacheManage
记录生活,分享技术~
个人博客:/What I want is to display a simple offline map using OpenStreetMap. I cannot find in the web the right tools to create map tiles and use it to display a map in Android. I have downloaded different resources but it seems that I don't have any idea where to start. I want to integrate images from OpenStreetMap using JOSM but i don't know if I can use it on Android.
Can I use Mapnik? Your help will a great thank you.
解决方案 I'm currently developing (my first) Android application using the OpenStreetMap (OSM) API, so while I can't help you with the JSOM, I can try to help with the OSM part:
Assuming that you want to create a new activity in your android application that simply displays a OSM map, you might start with something like this:
package example.stackoverflow.
import android.app.A
import android.os.B
import org.osmdroid.tileprovider.tilesource.TileSourceF
import org.osmdroid.util.GeoP
import org.osmdroid.views.MapV
public class YourMap extends Activity {
// The MapView variable:
private MapView m_mapV
// Default map zoom level:
private int MAP_DEFAULT_ZOOM = 15;
// Default map Latitude:
private double MAP_DEFAULT_LATITUDE = 38.535350;
// Default map Longitude:
private double MAP_DEFAULT_LONGITUDE = -121.753807;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Specify the XML layout to use:
setContentView(R.layout.osm_map);
// Find the MapView controller in that layout:
m_mapView = (MapView) findViewById(R.id.mapview);
// Setup the mapView controller:
m_mapView.setBuiltInZoomControls(true);
m_mapView.setMultiTouchControls(true);
m_mapView.setClickable(true);
m_mapView.setUseDataConnection(false);
m_mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
m_mapView.getController().setCenter(
new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
m_mapView.setTileSource(TileSourceFactory.MAPNIK);
} // end onCreate()
} // end class YourMap
Where your osm_map.xml layout may look something like this:
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"&
&org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:enabled="true"
android:clickable="true"
&/RelativeLayout&
As for the actual map tiles, there is a really cool program called Mobile Atlas Creator, which allows you to generate the necessary map tiles for the offline Android map implemented above.
Once you have the application installed, and you want to create a new atlas, you'll be asked to select your "desired altas format." When this pops up, select "Osmdroid zip."
Once everything loads, select a region on the map that you would like to create tiles for, select the zoom levels you want tiles for, and hit the "Add Selection" button in the column on the left, followed by "Create Atlas."
Oh, and depending on the source, you may need to check the "Create/Adjust Map tiles" checkbox to force the tiles to be exported as PNGs -- does anyone know if it's possible to use JPG with OSM?
Once the ZIP has been generated, I renamed it to "Mapnik.zip" and moved it to a newly created folder called "tiles" in my Eclipse Android project workspace. In order to get it working, I also had to open the zip file, and rename the top level folder from something like "Google Earth" (depending on the map source you used), to "Mapnik," in order for the tile to display in my Android application.
In order to actually load the tiles onto your phone, however, you'll need to use the ADB tool from the terminal. In your ADB tool directory, you'll want to run something like this (each line is a new command):
./adb shell rm -r /sdcard/osmdroid/
./adb shell mkdir /sdcard/osmdroi/
./adb push ~/path/to/your/mapnik.zip /sdcard/osmdroid
Depending on the size of the map and the speed of the phone's memory bus, this last step may take a several minutes to an hour to complete. Once done, your map should work -- I hope!
As I mentioned, this is the first time I've used the OSM API, so I'm by no means an expert on it, and I can only comment on what worked for me.
Hope this will help you get started!
I didn't have a chance to actually run the code that I wrote up yesterday, so I didn't catch a few of the errors. I just created a new project in Eclipse, dumped my code in there, fixed a few things and got it up and running. All changes that I made are reflected in the code above! I forgot several of the basic import statements, and I forgot to add permissions to the manifest.xml file.
The last few lines of my manifest.xml now look like this:
&uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&
&uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&
&uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&
&uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&
&uses-permission android:name="android.permission.INTERNET" /&
&uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&
&/manifest&
And you also might want to add this to the manifest, although certainly not critical:
&supports-screens
android:anyDensity="true"
android:resizeable="false"
android:largeScreens="true"
android:normalScreens="true"
Add this right after the &uses-sdk ... /& part.
Furthermore, be sure to import the following two JAR libraries:
osmdroid-android-3.0.3.jar // Or whatever version you're using...
slf4j-android-1.5.8.jar // Or whatever the latest version is...
Without this last JAR, my code kept crashing until I remembered to include it.
Make sure to modify the default coordinates so that they point to a location that you actually have map tiles for, otherwise you're not going to see much of anything, aside from a white canvas.
Sorry for not running the code on my end first!
本文地址: &
我要的是显示使用OpenStreetMap的一个简单的离线地图。我找不到在Web合适的工具来创建地图图块,并用它来显示Android的地图。我已经下载了不同的资源,但似乎我没有任何想法从哪里开始。我想用集成JOSM从OpenStreetMap的图像,但我不知道我是否可以在Android上使用它。
我可以使用Mapnik的?您的帮助会很大的感谢你。
解决方案 我目前正在开发(我第一次)使用OpenStreetMap的(OSM)API,因此,虽然我不能帮你的JSOM Android应用程序,我可以尽力帮助与OSM部分:
假设你想创建你的android应用程序中的新的活动,仅仅显示一个OSM的地图,你可能会开始是这样的:
包example.stackoverflow.
进口android.app.A
进口android.os.B
进口org.osmdroid.tileprovider.tilesource.TileSourceF
进口org.osmdroid.util.GeoP
进口org.osmdroid.views.MapV
公共类YourMap延伸活动{
//的MapView变量:
私人MapView类m_mapV
//默认的地图缩放级别:
私人诠释MAP_DEFAULT_ZOOM = 15;
//默认的地图纬度:
私人双人MAP_DEFAULT_LATITUDE = 38.535350;
//默认的地图经度:
私人双人MAP_DEFAULT_LONGITUDE = -121.753807;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//指定XML布局的使用方法:
的setContentView(R.layout.osm_map);
//查找该布局的MapView控制器:
m_mapView =(图形页面)findViewById(R.id.mapview);
//设置的MapView控制器:
m_mapView.setBuiltInZoomControls(真正的);
m_mapView.setMultiTouchControls(真正的);
m_mapView.setClickable(真正的);
m_mapView.setUseDataConnection(假);
m_mapView.getController()setZoom(MAP_DEFAULT_ZOOM)。
m_mapView.getController()。setCenter(
新的GeoPoint(MAP_DEFAULT_LATITUDE,MAP_DEFAULT_LONGITUDE));
m_mapView.setTileSource(TileSourceFactory.MAPNIK);
} //结束的onCreate()
} //结束类YourMap
如果您osm_map.xml布局可能会是这个样子:
< XML版本=“1.0”编码=“UTF-8”&GT?;
< RelativeLayout的的xmlns:机器人=“/apk/res/android”
机器人:layout_width =“match_parent”
机器人:layout_height =“match_parent”>
< org.osmdroid.views.MapView
机器人:ID =“@ + ID /图形页面”
机器人:layout_width =“match_parent”
机器人:layout_height =“match_parent”
机器人:启用=“真”
机器人:可点击=“真”
< / RelativeLayout的>
至于实际的地图图块,有一个叫很酷程序的移动阿特拉斯造物主的,它可以生成必要的地图图块的离线Android的地图上面实现。
一旦你安装的应用程序,您要创建一个新的地图,你会被要求选择你的“所需的阿尔塔斯格式。”当此弹出,选择“Osmdroid拉链。”
在所有负载,在地图上选择一个区域,你想创建瓷砖,选择您想要瓷砖的缩放级别,并点击“添加选择”按钮,在左边的列,其次是“创建阿特拉斯“。
哦,根据来源,您可能需要选中“创建/调整地图图块”复选框来强制导出为PNG格式的瓷砖 - 没有人知道是否有可能使用JPG与OSM?
在该ZIP已经产生,我给它改名为“Mapnik.zip”,并把它移到一个在我的Eclipse Android项目工作区被称为“砖”新建文件夹。为了得到它的工作,我也有过,以便打开压缩文件,并重新命名的类似“谷歌地球”的顶层文件夹(这取决于你使用的地图源),以“Mapnik的,”为瓦在我的Android应用程序的显示。
为了实际加载的瓷砖到您的手机,但是,你需要从终端使用ADB工具。在您的ADB工具的目录,你想运行这样的事情(每一行是一个新的命令):
./亚行外壳RM -R / SD卡/ osmdroid /
./adb壳的mkdir / SD卡/ osmdroi /
./adb推?/路径/要/你/ mapnik.zip / SD卡/ osmdroid
根据地图的大小和手机的存储器总线的速度,这最后一步可能需要几分钟到一个小时内完成。一旦这样做,你的地图应该努力 - !我希望
正如我所说,这是我第一次用的OSM API,所以我绝不是它的专家,我可以为我工作仅仅注释。
希望这将帮助你开始!
我没有机会实际运行code,我昨天写了,所以我没赶上了几个错误。我刚刚创建了一个新的项目在Eclipse中,甩我的code。在那里,定了一些东西,得到它运行起来。我的所有更改会反映在code以上!我忘了一些基本的import语句,我忘了权限添加到manifest.xml文件。
我的manifest.xml的最后几行现在看起来是这样的:
<使用-权限的Android:名称=“android.permission.ACCESS_COARSE_LOCATION”/>
<使用-权限的Android:名称=“android.permission.ACCESS_FINE_LOCATION”/>
<使用-权限的Android:名称=“android.permission.ACCESS_WIFI_STATE”/>
<使用-权限的Android:名称=“android.permission.ACCESS_NETWORK_STATE”/>
<使用-权限的Android:名称=“android.permission.INTERNET对”/>
<使用-权限的Android:名称=“android.permission.WRITE_EXTERNAL_STORAGE”/>
< /舱单>
和您可能还需要将它添加到清单,但肯定不是重要的:
<支持屏
机器人:anyDensity =“真”
机器人:调整大小=“假”
机器人:largeScreens =“真”
机器人:normalScreens =“真”
在添加这一权利的<使用的SDK ... /> 部分。
此外,一定要导入以下两个JAR库:
osmdroid-的android-3.0.3.jar //或者你使用的是什么版本?
SLF4J-的android-1.5.8.jar //或任何最新版本是...
如果没有这最后的JAR,我的code保持崩溃,直到我记得把它列入。
确认,使它们指向你实际有地图图块的位置,修改默认的坐标,否则你不会看到任何东西,除了一个白色的画布。
对不起,没有运行在我结束在code第一!
本文地址: &
扫一扫关注官方微信国内有公司使用OpenStreetMap吗? - 知乎40被浏览6211分享邀请回答/html/folder/946895-1.htm这里列表中带有文件夹图标的项,详情页都会有OSM使用)。如果说科研院所等机构不算公司的话,那么这个例子还真不是由院所本身提供的——这里OSM标准瓦片被以OpenLayers的形式直接显示,在此基础上显示了网站相关的保护区KML面图层。而整个OpenLayers的地图控件是嵌在一个由“北京宏图远见科技有限公司”提供的域名下的iframe内。然而请求的底图瓦片直接来自OSM的瓦片服务器({a,b,c}.),可能不是非常符合OSM的。因为基于OSM瓦片服务器开发其他与OSM编辑无关的应用程序需要事先联系OSM管理员,当然看起来这个站的访问量很有限,不足以对OSM正常编辑构成威胁,所以这种行为是不是算作重度使用还不明确。==========更新前一段偶然看到天地图服务指南里写道:提供覆盖全球,数据精细的矢量地图和影像地图。然后就随手去看了一下国外的部分,放大到一定级别的时候,屏幕下方出现了数据来源:天地图& OpenStreetMap contributor的字样。这算是我第一次见到国内公司使用OpenStreetMap的案例。作为国家队,即便使用了这样的开放数据也还是可以拿到审图号,虽然国内完全没用OSM的数据,但是这一举动说明至少没有从政策上宣判OSM在国内的死刑,幸甚至哉。=====分割线=====--以下为回答原文--据我所知国内没有公司使用OpenStreetMap数据发布电子地图产品或提供导航服务。开放和免费是优点免费是优点,但开放在我看来并非优点。开放编辑带来的问题是数据质量和一致性不能保证,开放授权(即ODbL协议)带来的问题是"Share Alike",即数据使用者必须以相同方式共享所用数据本身以及演绎得到的数据。最近的一篇用户日志总结了一份详尽报告中指出的目前该协议对OSM使用状况的不良影响。中国的地图似乎不够新愚以为OSM的中国数据不是不够新,而是不够完整和详尽。由于开放编辑的特性,据我所知,有一些维基百科的编辑者同时也是OSM的贡献者,尤其是铁路等主题的编辑者经常更新OSM数据并将链接添加到维基百科条目中。然而,并非所有类型和所有地区的数据均有贡献者在维护或编辑,比如显而易见,国家高速网络和省道网络并不完整,更不用说城市道路的完整度了。法律上的问题这是显然的,一定程度上也是OSM中国区不能很好发展的原因。根据《》《》和《》,在中华人民共和国境内公开出版地图、引进地图、展示、登载地图以及在生产加工的产品上附加的地图图形的审核,应当遵守本规定。下列情况下,单位和个人(以下统称申请人),应当按照本规定向地图审核部门提出地图审核申请:(一)在地图出版、展示、登载、引进、生产、加工前;(二)使用国务院测绘行政主管部门或者省级测绘行政主管部门提供的标准画法地图,并对地图内容进行编辑改动的。受理地图审核申请时,受理窗口除要求申请人按照《地图审核管理规定》提交有关材料外,还应当提交下列材料:(四)引进的境外地图,需提交引进地图的相关证明材料;互联网地图服务资质单位应当使用经省级以上测绘地理信息行政主管部门审核批准的地图提供服务,并将地图审图号、测绘资质证书编号、互联网信息服务增值电信业务经营许可证编号共同标注在网站显著位置。显然在国内使用OpenStreetMap数据并不能满足上述法律要求。OpenStreetMap Wiki的页面也有法律警示。178 条评论分享收藏感谢收起}

我要回帖

更多关于 openstreetmap 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信