在android中离线的mapbox的简单示例不起作用

时间:2016-12-21 04:19:56

标签: android android-studio mapbox

我在Android Studio中创建了一个应用程序并安装了SDK应用程序 并且应用程序运行正常,没有任何问题,但是地图的建立将ProgressBar加载到中间并且显示我在这个短语的操作中我已经超出了所需的区块

这是XML     

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:center_latitude="37.73359"
    mapbox:center_longitude="-119.58410"
    mapbox:style_url="mapbox://styles/mapbox/outdoors-v9"
    mapbox:zoom="10"
    mapbox:zoom_min="10"/>

<ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:visibility="gone"/>

这是MainActivity

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";

private boolean isEndNotified;
private ProgressBar progressBar;
private MapView mapView;
private OfflineManager offlineManager;

// JSON encoding/decoding
public static final String JSON_CHARSET = "UTF-8";
public static final String JSON_FIELD_REGION_NAME = "FIELD_REGION_NAME";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    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(MainActivity.this);

            // Create a bounding box for the offline region
            LatLngBounds latLngBounds = new LatLngBounds.Builder()
                    .include(new LatLng(37.7897, -119.5073)) // Northeast
                    .include(new LatLng(37.6744, -119.6815)) // Southwest
                    .build();

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

            // Set the metadata
            byte[] metadata;
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(JSON_FIELD_REGION_NAME, "Yosemite National Park");
                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);
                        }
                    });
        }
    });
}

@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
    offlineManager.listOfflineRegions(new OfflineManager.ListOfflineRegionsCallback() {
        @Override
        public void onList(OfflineRegion[] offlineRegions) {
            if (offlineRegions.length > 0) {
                // delete the last item in the offlineRegions list which will be yosemite offline map
                offlineRegions[(offlineRegions.length - 1)].delete(new OfflineRegion.OfflineRegionDeleteCallback() {
                    @Override
                    public void onDelete() {
                        Toast.makeText(MainActivity.this, "Yosemite offline map deleted", Toast.LENGTH_LONG).show();
                    }

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

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

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

// Progress bar methods
private void startProgress() {

    // Start and show the progress bar
    isEndNotified = false;
    progressBar.setIndeterminate(true);
    progressBar.setVisibility(View.VISIBLE);
}

private void setPercentage(final int percentage) {
    progressBar.setIndeterminate(false);
    progressBar.setProgress(percentage);
}

private void endProgress(final String message) {
    // Don't notify more than once
    if (isEndNotified) {
        return;
    }

    // Stop and hide the progress bar
    isEndNotified = true;
    progressBar.setIndeterminate(false);
    progressBar.setVisibility(View.GONE);

    // Show a toast
    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}

}

this is problem

1 个答案:

答案 0 :(得分:0)

这不是错误,而是我们对用户施加的限制,因此他们无法立即下载所有地球。您可以阅读有关tile limit here的更多信息,并使用tile calculator来估算该地区。如果您想下载更大的区域,则需要将您的区域划分为更小的下载。