如何从 OpenStreetMap 离线创建地图图块并在 Android 上显示?
我想要的是使用 OpenStreetMap 显示一个简单的离线地图。我在网上找不到合适的工具来创建地图图块并使用它在 Android 中显示地图。我已经下载了不同的资源,但似乎我不知道从哪里开始。我想使用 JOSM 集成来自 OpenStreetMap 的图像,但我不知道是否可以在 Android 上使用它。
我可以使用 Mapnik 吗?您的帮助将非常感谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我目前正在使用 OpenStreetMap (OSM) API 开发(我的第一个)Android 应用程序,因此虽然我无法帮助您完成 JSOM,但我可以尝试帮助您完成 OSM 部分:
假设您想要创建一个新活动在仅显示 OSM 地图的 Android 应用程序中,您可能会从以下内容开始:
您的 osm_map.xml 布局可能如下所示:
至于实际的地图图块,有一个非常酷的程序,名为 Mobile Atlas Creator,它允许您为上面实现的离线 Android 地图生成必要的地图图块。
安装应用程序后,如果您想要创建新的图集,系统会要求您选择“所需的 Altas 格式”。弹出此窗口时,选择“Osmdroid zip”。
加载完所有内容后,在地图上选择您想要为其创建图块的区域,选择您想要图块的缩放级别,然后单击左侧列中的“添加选择”按钮,然后单击“创建图集”。
哦,根据来源,您可能需要选中“创建/调整地图图块”复选框以强制将图块导出为 PNG - 有谁知道是否可以将 JPG 与 OSM 一起使用?
生成 ZIP 后,我将其重命名为“Mapnik.zip”,并将其移动到 Eclipse Android 项目工作区中新创建的名为“tiles”的文件夹中。为了让它工作,我还必须打开 zip 文件,并将顶级文件夹从“Google Earth”(取决于您使用的地图源)之类的名称重命名为“Mapnik”,以便图块能够显示在我的 Android 应用程序中。
然而,为了将图块实际加载到您的手机上,您需要使用终端中的 ADB 工具。在您的 ADB 工具目录中,您需要运行如下所示的命令(每一行都是一个新命令):
根据地图的大小和手机内存总线的速度,最后一步可能需要几分钟才能完成小时完成。完成后,你的地图应该可以工作了——我希望如此!
正如我提到的,这是我第一次使用 OSM API,所以我绝不是这方面的专家,我只能评论对我有用的内容。
希望这能帮助您入门!
编辑:
我没有机会实际运行我昨天编写的代码,所以我没有发现一些错误。我刚刚在 Eclipse 中创建了一个新项目,将我的代码转储到其中,修复了一些问题并启动并运行。我所做的所有更改都反映在上面的代码中!我忘记了一些基本的导入语句,并且忘记向manifest.xml 文件添加权限。
我的 manifest.xml 的最后几行现在看起来像这样:
您可能还想将其添加到清单中,尽管这当然不是关键:
在
之后添加此内容
部分。此外,请务必导入以下两个 JAR 库:
如果
没有这最后一个 JAR,我的代码会一直崩溃,直到我记得包含它。
确保修改默认坐标,以便它们指向您实际拥有地图图块的位置,否则除了白色画布之外,您将看不到太多任何内容。
抱歉没有先在我这边运行代码!
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:
Where your osm_map.xml layout may look something like this:
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):
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!
EDIT:
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:
And you also might want to add this to the manifest, although certainly not critical:
Add this right after the
<uses-sdk ... />
part.Furthermore, be sure to import the following two JAR libraries:
and
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!
以下是分步解决方案:
简而言之:
1- 您必须使用 Mobile Atlas Creator 下载地图图块。我已经解释了步骤此处
2-将生成的zip文件移动到
/mnt/sdcard/osmdroid/< /code> 在您的设备上。
3-添加 osmdroid-android-XXX.jar 和 slf4j-android-1.5.8.jar 进入项目的构建路径
4- 添加 MapView:您可以将 MapView 添加到您的 xml 布局
或以编程方式创建 MapView:
不要忘记将这些权限添加到您的清单:
这是一个很好的 示例项目。
希望它有帮助;)
重要
正如@Scai提到的:最近开放街道地图宣布这个工具不好并且有一些问题:
Here is a step by step solution:
In brief:
1- You must download map tiles using Mobile Atlas Creator. I have explained the steps HERE
2- Move the resulting zip-file to
/mnt/sdcard/osmdroid/
on your device.3- Adding osmdroid-android-XXX.jar and slf4j-android-1.5.8.jar into build path your project
4- Adding MapView: You can add a MapView to your xml layout
Or create a MapView programmatically:
Don't forget to add these permissions to your Manifest:
This is a good Sample Project.
Hope it Helps ;)
Important
As @Scai mentioned: recently Open Street Map announced that this tool is not good and had some problems:
除了映射图块,您还可以将矢量数据用于离线地图,例如mapsforge 或Mapbox Android SDK。如需了解更多信息,请参阅有关 离线 OSM 和 Android 库。
Alternatively to map tiles you can also use vector data for your offline map, for example mapsforge or the Mapbox Android SDK. For more information consult the OSM wiki about offline OSM and Android libraries.