【问题标题】:Packaging Maps within an Android App?在 Android 应用程序中打包地图?
【发布时间】:2012-09-10 05:05:38
【问题描述】:

我对该主题进行了一些研究,并发现上次询问此问题(特别是针对 Google 地图)时,答案是“这是非法的”。既然 Google 地图现在为您提供了自己进行离线缓存的能力,为什么它仍然是非法的?

无论如何,我有一个应用程序,我需要为(几平方英里)打包一张小地图。我已经考虑过做 osmdroid,但很难理解他们在做什么。我查看了他们的 SVN 存储库并在 Play Store 上查看了他们的示例应用程序,但很失望。该应用程序本身几乎完全显示了我的需要,但它使用了一些奇怪的代码形式,未包含在 SDK 中。 osmdroid 似乎是我目前最好的选择,但似乎没有太多可用的参考资料。

如果有人能指出我开始使用 osmdroid 的正确方向,或者只是其他一些允许打包地图的 SDK,我将不胜感激。

注意:我已经尝试在 osmdroid 中执行此操作,但在执行时

 mapView.setUseDataConnection(false); 

地图没有加载任何内容,即使文件似乎位于正确的目录中(因为它也将所有文件下载到那里)

【问题讨论】:

  • “既然谷歌地图现在为您提供了自己进行离线缓存的能力,为什么它仍然是非法的?” -- 对于初学者,不支持。您将 Google Maps 应用程序(具有离线缓存)与适用于 Android 的 Google Maps SDK 插件(没有离线缓存)混淆了。它们与尼安德特人与克罗马侬人的关系大致相同。

标签: android google-maps osmdroid


【解决方案1】:

要像 Google Maps 一样使用 Osmdroid,您只需要在项目中添加几个 jar 文件,其中一个是 osmdroid-android-3.0.x.jar(对于 x - 我使用了版本 5 和版本 7)。另一个 jar 是 slf4j-android-1.5.8.jar。 (可能有这个的更高版本,不记得我从哪里得到它,但应该在某个地方的 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;

// This is all you need to display an OSM map using osmdroid
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.MAPNIK);
        mMapView.setBuiltInZoomControls(true);
        mMapController = mMapView.getController();
        mMapController.setZoom(13);
        GeoPoint gPt = new GeoPoint(51500000, -150000);
        //Centre map near to Hyde Park Corner, London
        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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <org.osmdroid.views.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:id="@+id/mapview"
        ></org.osmdroid.views.MapView>
</LinearLayout>
---------------------------------------------------------- XML END
Include slf4j-android-1.5.8.jar and osmdroid-android-3.0.5.jar in the build path
(Google search for where to get them from)
*/

对于离线缓存,请使用 Mobile Atlas creator (MOBAC) 制作感兴趣区域的 Osmdroid Zip 格式的 Atlas。我使用 OpenStreetMap 作为瓦片源。获得 zip 文件后,只需将其放入设备上的 Osmdroid 目录即可。它应该为您提供离线功能。我一直在伦敦自己的应用程序中使用它,并关闭数据包数据,以降低数据下载(及其成本)。

【讨论】:

  • 不幸的是,我的问题是我的图块文件是 .png 而不是 .png.tile。我的代码中的其他一切都很好。您对限制可视区域有什么建议吗?
  • 我自己并没有限制该区域,但我相信应该可以。当然,如果地图处于“GPS 居中模式”(即以 GPS 为您提供的地图居中),您可以在纬度/经度上设置界限。如果它处于平移模式(即不跟随 GPS,而是用户拖动地图),则可能会更困难。
  • 看来我可以用这个绑定它:code.google.com/p/osmdroid/issues/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2012-06-22
相关资源
最近更新 更多