在Android应用程序中打包地图?
我对这个话题做了一些研究,发现上一次有人问这个问题(特别是谷歌地图),答案是“这是非法的”。既然谷歌地图现在为你提供了离线缓存的能力,为什么它仍然是非法的呢 无论如何,我有一个应用程序,我需要打包一个小地图(几平方英里)。我曾研究过使用osmdroid,但很难了解他们在做什么。我查看了他们的SVN回购,并在Play Store上查看了他们的示例应用程序,感到失望。该应用程序本身几乎完全显示了我所需要的内容,但它使用了一些奇怪的代码形式,没有包含在SDK中。在这一点上,osmdroid似乎是我最好的选择,但似乎没有太多的参考资料可用 如果有人能为我指出开始使用osmdroid或其他一些允许打包映射的SDK的正确方向,我将不胜感激 注意:我曾尝试在osmdroid中执行此操作,但在执行此操作时在Android应用程序中打包地图?,android,google-maps,osmdroid,Android,Google Maps,Osmdroid,我对这个话题做了一些研究,发现上一次有人问这个问题(特别是谷歌地图),答案是“这是非法的”。既然谷歌地图现在为你提供了离线缓存的能力,为什么它仍然是非法的呢 无论如何,我有一个应用程序,我需要打包一个小地图(几平方英里)。我曾研究过使用osmdroid,但很难了解他们在做什么。我查看了他们的SVN回购,并在Play Store上查看了他们的示例应用程序,感到失望。该应用程序本身几乎完全显示了我所需要的内容,但它使用了一些奇怪的代码形式,没有包含在SDK中。在这一点上,osmdroid似乎是我最好
mapView.setUseDataConnection(false);
地图不会加载任何内容,即使文件似乎位于正确的目录中(因为它也会在那里下载所有文件)要使用类似于谷歌地图的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)
*/
包osmdemo.demo;
导入org.osmdroid.tileprovider.tilesource.TileSourceFactory;
导入org.osmdroid.util.GeoPoint;
导入org.osmdroid.views.MapController;
导入org.osmdroid.views.MapView;
导入android.app.Activity;
导入android.os.Bundle;
//这就是使用osmdroid显示OSM映射所需的全部内容
公共类OsmdroidDemoMap扩展活动{
私人地图视图;
专用地图控制器mMapController;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.osm_main);
mMapView=(MapView)findViewById(R.id.MapView);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(真);
mMapController=mMapView.getController();
mMapController.setZoom(13);
地质点gPt=新地质点(51500000,-150000);
//伦敦海德公园角附近的中心地图
mMapController.setCenter(gPt);
}
}
/*将此作为您的osm_main.xml
----------------------------------------------------------XML开始
----------------------------------------------------------XML端
在构建路径中包括slf4j-android-1.5.8.jar和osmdroid-android-3.0.5.jar
(谷歌搜索从哪里获取)
*/
至于离线缓存,请使用Mobile Atlas creator(MOBAC)以感兴趣区域的Osmdroid Zip格式制作一个Atlas。我使用OpenStreetMap作为平铺源。一旦你有了zip文件,就把它放到你设备上的Osmdroid目录中。它应该给你离线的能力。为了降低数据下载(及其成本),我在伦敦自己的应用程序中一直使用它,并关闭数据包数据。不幸的是,我的tile文件是.png而不是.png.tile。我的代码中的其他一切都非常好。关于限制可视区域,你有什么建议吗?我自己并没有限制这个区域,但我相信这是可能的。当然,如果地图处于“GPS居中模式”(即将地图居中于GPS提供的位置),您可以在纬度/经度上设置边界。如果它处于pan模式(即不跟随GPS,但用户拖动地图),可能会更困难。看起来我可以用这个来绑定它:“既然谷歌地图现在为您提供了离线缓存的能力,为什么它仍然是非法的?”——首先,它不受支持。您将Google Maps应用程序(具有脱机缓存)与Google Maps SDK Android插件(不具有脱机缓存)混淆。他们与尼安德特人和克罗马侬人的关系大致相同。