2016-11-25 85 views
1

我想在我的应用程序中下载特定的城市。如何做到这一点?还有一个问题:当我使用Mapbox 4.1.1的SDK时,我无法添加类BoundingBox如何在Android中使用离线地图

我有一个问题,code that exists on their site下载地图不是基于一次。我必须停止运行程序,并且当我重新运行它时,地图不会再次加载。 这是我的代码:指定是超过6000瓦数限制

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

}

+0

OpenStreetMap你可以使用。 – Saveen

+0

在用什么? –

+1

从这里看看https://github.com/osmdroid/osmdroid – Saveen

回答

2

您的离线区域。您可以在我们的help pages上阅读更多信息,并使用tile calculator来减小区域大小或更改下载的缩放级别。