我正在寻找一份教程/手册或步骤,以将Open street map包含到我的安卓应用程序中。我发现的要么是一个拥有更多功能的大项目,否则就是很多问题没有得出“如何”正确的结论..!
是否有任何适合新手参考的正确博客/网站或文档?
我正在寻找一份教程/手册或步骤,以将Open street map包含到我的安卓应用程序中。我发现的要么是一个拥有更多功能的大项目,否则就是很多问题没有得出“如何”正确的结论..!
是否有任何适合新手参考的正确博客/网站或文档?
我不知道是否有任何教程,但这是我为使用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)
*/
请注意,为避免无法从OSM下载图块,您现在必须使用最新版本(4.1)。
另外请注意,他们正在将其存储库迁移到Github,但该过程尚未完成。此页面下载包含jar文件的链接。
这个 OSMdroid 的示例项目 绝对是我找到的最简单的。只需要5分钟就可以使用,注意清单文件。
为了更复杂一些,这个教程显示带有当前地理点的地图。
这里有一些来自各种项目的代码片段,虽然没有测试所有的内容。
你应该下载OSMdroid和SLF4J,将其放置在libs
文件夹中,将 osmdroid 添加为库,使用适当的权限修复 AndroidManifest.xml(见第一个教程)。如果你这样做了,就没有必要像第一个教程中推荐的那样修改 gradle.build
文件。
我建议您在使用这些内容时,首先使用较旧版本的 osmdroid (3.x)。一旦您感到舒适,就迁移到较新版本的 osmdroid (4.x 或 5.x)。
在较新版本中 GeoPoint 和 MapController 类更改了名称,因此要注意不兼容类型错误:
IGeoPoint 无法转换为 GeoPoint
IMapController 无法转换为 MapController
/mnt/sdcard/osmdroid/
完成以上步骤后,一切都与@Nick解释的相同。OSM 真的很棒,但是使用起来特别困难,尤其是它的 Google API,而且没有太多的教程和文档。最好的选择是使用像 mapbox 这样的服务,它们有很棒的文档和大量资源,易于使用且价格实惠。希望这能帮到你。