简体   繁体   English

使用MapBox Android SDK进行离线磁贴缓存

[英]Offline tile caching using MapBox Android SDK

I have a working iOS prototype using the iOS tile-caching technique as shown below (Objective-C code): 我有一个使用iOS平铺缓存技术的iOS原型,如下所示(Objective-C代码):

RMTileCache  * tileCache = [[RMTileCache alloc] initWithExpiryPeriod:0]; 
[tileCache setBackgroundCacheDelegate:self]; 
RMMapboxSource * tileSource = [[RMMapboxSource alloc] initWithMapID:mapID]; 
[tileCache beginBackgroundCacheForTileSource:tileSource southWest:southWest northEast:northEasth minZoom:minZoom maxZoom:maxZoom];

What this basically does is download the map, cache the tiles permanently and make it possible for the app to run offline in the future. 这基本上做的是下载地图,永久缓存切片并使应用程序可以在将来脱机运行。 Since we're going through the official payed API, this is of course not violating any of the legal restrictions. 由于我们正在通过官方付费API,这当然不会违反任何法律限制。

Now I'd like to achieve the same on Android. 现在我想在Android上实现同样的目标。 I have the SDK running in Android Studio and a working project with a remote map using the Map ID, basically this (Android Eclipse layout XML): 我有在Android Studio中运行的SDK和一个使用Map ID的远程地图的工作项目,基本上这是(Android Eclipse布局XML):

<com.mapbox.mapboxsdk.views.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    mapid=“my_map_id" />

This works fine, but the solution has to be completely offline once the caching is done. 这工作正常,但一旦缓存完成,解决方案必须完全脱机。 My question is: is there a Java equivalent of the iOS source code above in the MapBox SDK? 我的问题是:MapBox SDK中是否有上述iOS源代码的Java等价物? I attempted to look in the API, but could not find a solid reference to the tile caching system. 我试图查看API,但找不到对tile缓存系统的可靠引用。 And after some painful time trying to get it running based on the method names and code documentation, I gave up. 经过一些痛苦的时间试图让它运行基于方法名称和代码文档,我放弃了。

I'm running the latest GitHub distribution of MapBox along with the latest Android Studio, everything's up and running fine, but can't find the code to accomplish this. 我正在运行MapBox的最新GitHub发行版以及最新的Android Studio,所有内容都运行良好,但无法找到完成此任务的代码。 I don't necessarily need an API reference, a few lines of code showing how it's done would be enough. 我不一定需要API参考,几行代码显示它是如何完成的。

Offline Tile Caching support is now available in the Mapbox Android SDK as of version 0.5.1. 从版本0.5.1开始,Mapbox Android SDK现在提供离线平铺缓存支持。 It was released on 20-December-2014. 它于2014年12月20日发布。 Here's a basic example of how to get started: 以下是如何入门的基本示例:

OfflineMapDownloader offlineMapDownloader = OfflineMapDownloader.getOfflineMapDownloader(getActivity());
BoundingBox boundingBox = mapView.getBoundingBox();
CoordinateSpan span = new CoordinateSpan(boundingBox.getLatitudeSpan(), boundingBox.getLongitudeSpan());
CoordinateRegion coordinateRegion = new CoordinateRegion(mapView.getCenter(), span);
offlineMapDownloader.beginDownloadingMapID("MapboxMapID", coordinateRegion, (int) mapView.getZoomLevel(), (int) mapView.getZoomLevel());

To load a previously saved map: 要加载以前保存的地图:

ArrayList<OfflineMapDatabase> offlineMapDatabases = offlineMapDownloader.getMutableOfflineMapDatabases();
OfflineMapDatabase db = offlineMapDatabases.get(0);
OfflineMapTileProvider tp = new OfflineMapTileProvider(getActivity(), db);
offlineMapOverlay = new TilesOverlay(tp);
mapView.addOverlay(offlineMapOverlay);

I asked this question to the support team, here's the answer: 我向支持团队提出这个问题,这是答案:

"We don't currently have a release date for the Android SDK or for this feature, because both are in very early stages of development. “我们目前没有Android SDK或此功能的发布日期,因为两者都处于开发的早期阶段。

-- Tom MacWright support@mapbox.com" - Tom MacWright support@mapbox.com“

It's a very good product, I hope we can use it soon in Android. 这是一个非常好的产品,我希望我们可以很快在Android中使用它。

In your layout file there must be: 在您的布局文件中必须有:

<com.mapbox.mapboxsdk.views.MapView
        android:id="@+id/yourMapViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

In code where you want to initialize MapView : 在要初始化MapView代码中:

File file = new File("your full path to tiles db");
MBTilesLayer mbTilesLayer = new MBTilesLayer(file);
MapView mapView = (MapView) findViewById(R.id.yourMapViewId);
mapView.setTileSource(mbTilesLayer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM