如何在 android 应用程序中使用 OSM 地图。?是否有任何教程可以学习在 android 中使用 OSM。?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22295768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use OSM map in an android application.? Is there any tutorial to learn about using OSM in android.?
提问by rubin
Hi guys from past one week i'm searching for a tutorial/manual or steps to include Open street map into my android application. All i found is either a big project with lot more functionality on it, otherwise so many questions ended without proper conclusion about "HOW"..!
大家好,过去一周我正在寻找教程/手册或步骤,以将开放街道地图包含到我的 android 应用程序中。我所发现的要么是一个具有更多功能的大项目,否则很多问题都没有关于“如何”的正确结论而结束......!
Is there any proper blog/site or document that can a fresher can refer.?
有没有合适的博客/网站或文档可以让新人参考。?
回答by NickT
I don't know of any tutorials but here's the code I wrote for a minimal example using Osmdroid.
我不知道任何教程,但这是我为使用 Osmdroid 的最小示例编写的代码。
// This is all you need to display an OSM map using osmdroid
package osmdemo.demo;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.os.Bundle;
public class OsmdroidDemoMap extends Activity {
private MapView mMapView;
private MapController mMapController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.osm_main);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
mMapView.setBuiltInZoomControls(true);
mMapController = (MapController) mMapView.getController();
mMapController.setZoom(13);
GeoPoint gPt = new GeoPoint(51500000, -150000);
mMapController.setCenter(gPt);
}
}
/* HAVE THIS AS YOUR osm_main.xml
---------------------------------------------------------- XML START
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<org.osmdroid.views.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
---------------------------------------------------------- XML END
Include slf4j-android-1.5.8.jar and osmdroid-android-4.1.jar in the build path
(Google search for where to get them from)
*/
Note that you must now use the latest version (4.1) to avoid getting blocked from downloading tiles from OSM.
请注意,您现在必须使用最新版本 (4.1) 以避免被阻止从 OSM 下载图块。
Also note that they are moving their repositries to Github and the process isn't complete yet. This page downloadsholds the links for the jars
另请注意,他们正在将其存储库移至 Github,并且该过程尚未完成。此页面下载包含罐子的链接
回答by tony gil
This OSMdroid sample projectis definitely the simplest that i have come across. No more than 5 minutes to be up and running. Pay attention to the manifest file.
这个 OSMdroid 示例项目绝对是我遇到的最简单的项目。启动和运行不超过 5 分钟。注意清单文件。
For a bit more complexity, this tutorialdisplays a map with a current geopoint.
为了更复杂一点,本教程显示了一个带有当前地理点的地图。
Here are some snippetsfrom various projects. Haven't tested all of them, though.
You should download OSMdroidand SLF4J, place in libs
folder, Add as Library
osmdroid, fix AndroidManifest.xml with proper permissions (see first tutorial). If you do this, there is no need to alter gradle.build
file as recommended in first tutorial.
您应该下载OSMdroid和SLF4J,放在libs
文件夹Add as Library
osmdroid 中,使用适当的权限修复 AndroidManifest.xml(请参阅第一个教程)。如果您这样做,则无需gradle.build
按照第一个教程中的建议更改文件。
I recommend that you use, for starters, the older versions of osmdroid (3.x) when using these. Once you are comfortable, migrate to newer version of osmdroid (4.x or 5.x).
对于初学者,我建议您在使用这些时使用旧版本的 osmdroid (3.x)。适应后,迁移到较新版本的 osmdroid(4.x 或 5.x)。
GeoPoint and MapController classes change names in newer versions, so watch out for INCOMPATIBLE TYPES ERROR
GeoPoint 和 MapController 类在较新版本中更改名称,因此请注意不兼容的类型错误
IGeoPoint cannot be converted to GeoPoint
IMapController cannot be converted to MapController
IGeoPoint 无法转换为 GeoPoint
IMapController 无法转换为 MapController
回答by Milad Faridnia
I have explained the steps HERE. I also recommend you take a look at their Sample Projectwhich is very useful. And their documentation about HOW;)
我已经解释了这里的步骤。我还建议您查看他们的示例项目,这非常有用。以及他们关于HOW的文档;)
And for Offline Use
而对于脱机使用
1- you must download the map using MOBAC.
1- 您必须使用MOBAC下载地图。
2- Place it into /mnt/sdcard/osmdroid/
2- 将其放入 /mnt/sdcard/osmdroid/
After these steps everything is the same as @Nick explained.
在这些步骤之后,一切都与@Nick 解释的一样。
回答by Nirab Pudasaini
One mistake that people using Google Maps like service make is that they treat OpenStreetMap as just map tile provider. You need to know that OpenStreetMap is actually a big free editable wiki geodatabase and there are three different ways you can use this database.
人们使用 Google Maps 之类的服务所犯的一个错误是,他们将 OpenStreetMap 视为只是地图图块提供者。您需要知道 OpenStreetMap 实际上是一个大型的免费可编辑 wiki 地理数据库,您可以通过三种不同的方式使用该数据库。
You can make tools to write to this database. There is REST Api https://wiki.openstreetmap.org/wiki/API_v0.6. This is what you use if you have to make changes to the database. This is the least common use case but is most thoroughly documented. The API is main OSM API and can be used to download data as well but it is not the recommended way to download OSM data.
Next you can read data from this database. The best way to do is to download planet extracts https://wiki.openstreetmap.org/wiki/Planet.osmand smaller extracts hosted by geofabrik http://download.geofabrik.de/. If you want to make more specific queries, best options could be Overpass API https://wiki.openstreetmap.org/wiki/Overpass_API.
Finally there are a range of service that are built on top of OSM data like (map tiles, routing, POI search, geocoding). This is most probably what you are looking for there are lot of options for this. For Android a good complete solution is Mapbox https://www.mapbox.com/android-docs/map-sdk/overview/. The great thing about mapbox android sdk is that they provide everything like map tiles, routing, POI search, geocoding, offline maps and even live traffic using one service. Also you can follow the above link and look at tutorials and sample code. One bad thing is that you will need to use mapbox service and there is a risk of vendor lock in, but most of mapbox products are open source. If you work a little hard you should be able to piece together other solutions for yourself as well.
您可以制作工具来写入此数据库。有 REST Api https://wiki.openstreetmap.org/wiki/API_v0.6。如果您必须对数据库进行更改,这就是您使用的方法。这是最不常见的用例,但记录最完整。API 是主要的 OSM API,也可用于下载数据,但它不是下载 OSM 数据的推荐方式。
接下来,您可以从此数据库中读取数据。最好的方法是下载行星提取物https://wiki.openstreetmap.org/wiki/Planet.osm和由 geofabrik http://download.geofabrik.de/托管的较小提取物。如果您想进行更具体的查询,最好的选择可能是 Overpass API https://wiki.openstreetmap.org/wiki/Overpass_API。
最后,还有一系列建立在 OSM 数据之上的服务,例如(地图图块、路线、POI 搜索、地理编码)。这很可能是您正在寻找的内容,有很多选择。对于 Android,一个很好的完整解决方案是 Mapbox https://www.mapbox.com/android-docs/map-sdk/overview/。mapbox android sdk 的伟大之处在于它们使用一种服务提供所有内容,例如地图图块、路线、POI 搜索、地理编码、离线地图甚至实时交通。您也可以按照上面的链接查看教程和示例代码。一件坏事是你将需要使用 mapbox 服务并且存在供应商锁定的风险,但大多数 mapbox 产品都是开源的。如果您努力一点,您也应该能够为自己拼凑其他解决方案。
Hope this helps.
希望这可以帮助。
回答by narcis dpr
OSM is really great but hard to use specially with it's google api and also there isn't much tutorials and docs for it, the best option will be using services like mapboxwhich have great docs and a lot of sources, easy to use and affordable. hope this will help.
OSM 真的很棒,但很难特别使用它的 google api,而且没有太多的教程和文档,最好的选择是使用像mapbox这样的服务,它有很好的文档和大量的资源,易于使用且价格合理. 希望这会有所帮助。