如何在android中使用离线地图

时间:2016-11-25 12:15:06

标签: android mapbox

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

我遇到问题,下载地图的code that exists on their site不仅仅基于一次。我必须停止运行该程序,当我重新运行它时,地图不会再次加载。 这是我的代码:

// 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);
        }
      });
  }
});

}

2 个答案:

答案 0 :(得分:2)

指定的离线区域超过了6000个图块计数限制。您可以在help pages上详细了解相关信息,并使用tile calculator缩小区域大小或更改下载的缩放级别。

答案 1 :(得分:0)

或者您可以在新版本上使用 setOfflineMapboxTileCountLimit

    offlineManager.setOfflineMapboxTileCountLimit(20000000);