如何在Android中使用离线地图 [英] How using offline map in android

查看:252
本文介绍了如何在Android中使用离线地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中下载特定的城市.怎么办呢?还有另一个问题:当我使用Mapbox 4.1.1的SDK时,无法添加类BoundingBox.

I want download a particular city in my app. How can do this? There is another problem, too: when I use SDK of Mapbox 4.1.1 I can't add the class BoundingBox.

我有问题,他们网站上存在的代码下载地图不仅基于一次.我必须停止运行该程序,并且当我重新运行该程序时,不会再次加载该地图. 这是我的代码:

I have a problem, a code that exists on their site to download the map is not based just once. I have to stop running the program, and when I re-run it the map does not load again. This is my code :

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
MapboxAccountManager.start(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the account manager
setContentView(R.layout.activity_offline_simple);

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
  @Override
  public void onMapReady(MapboxMap mapboxMap) {
    // Set up the OfflineManager
    offlineManager = OfflineManager.getInstance(SimpleOfflineMapActivity.this);

    // Create a bounding box for the offline region
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(13.1,32.6)) // Northeast
      .include(new LatLng(13.6,32.9)) // Southwest
      .build();

    // Define the offline region
    OfflineTilePyramidRegionDefinition definition = new OfflineTilePyramidRegionDefinition(
      mapView.getStyleUrl(),
      latLngBounds,
      10,
      20,
      SimpleOfflineMapActivity.this.getResources().getDisplayMetrics().density);

    // Set the metadata
    byte[] metadata;
    try {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put(JSON_FIELD_REGION_NAME, "Triopli Libya");
      String json = jsonObject.toString();
      metadata = json.getBytes(JSON_CHARSET);
    } catch (Exception exception) {
      Log.e(TAG, "Failed to encode metadata: " + exception.getMessage());
      metadata = null;
    }

    // Create the region asynchronously
    offlineManager.createOfflineRegion(
      definition,
      metadata,
      new OfflineManager.CreateOfflineRegionCallback() {
        @Override
        public void onCreate(OfflineRegion offlineRegion) {
          offlineRegion.setDownloadState(OfflineRegion.STATE_ACTIVE);

          // Display the download progress bar
          progressBar = (ProgressBar) findViewById(R.id.progress_bar);
          startProgress();

          // Monitor the download progress using setObserver
          offlineRegion.setObserver(new OfflineRegion.OfflineRegionObserver() {
            @Override
            public void onStatusChanged(OfflineRegionStatus status) {

              // Calculate the download percentage and update the progress bar
              double percentage = status.getRequiredResourceCount() >= 0
                ? (100.0 * status.getCompletedResourceCount() / status.getRequiredResourceCount()) :
                0.0;

              if (status.isComplete()) {
                // Download complete
                endProgress("Region downloaded successfully.");
              } else if (status.isRequiredResourceCountPrecise()) {
                // Switch to determinate state
                setPercentage((int) Math.round(percentage));
              }
            }

            @Override
            public void onError(OfflineRegionError error) {
              // If an error occurs, print to logcat
              Log.e(TAG, "onError reason: " + error.getReason());
              Log.e(TAG, "onError message: " + error.getMessage());
            }

            @Override
            public void mapboxTileCountLimitExceeded(long limit) {
              // Notify if offline region exceeds maximum tile count
              Log.e(TAG, "Mapbox tile count limit exceeded: " + limit);
            }
          });
        }

        @Override
        public void onError(String error) {
          Log.e(TAG, "Error: " + error);
        }
      });
  }
});

}

推荐答案

您指定的离线区域超过了6000个图块计数限制.您可以在我们的帮助页面中了解更多信息.并使用 tile计算器来减小区域大小或更改下载的缩放级别.

Your offline region specified is exceeding the 6000 tile count limit. You can read more about this on our help pages and use the tile calculator to either reduce the region size or change the zoom levels downloaded.

这篇关于如何在Android中使用离线地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆