4
votes

for our app, I'm currently integrating map box with a custom map tile surce (as described here). Everything runs fine with a working internet connection, using the OfflineManager and OfflineTilePyramidRegionDefinition I can download tiles and find them in the mbgl-offline.db but they appear to not be used in the app. The offline regions are reported to be complete, but just don't show up. As I understand the offline documentation, after downloading tiles, everything else is "hands off".

I've tried several different sources (e.g., OpenMapTiles.org), as we are still in the process of setting up our own map tile server.

Am I missing something here? I really appreciate any leads.

Best, Phil

Update: Here's some more information:

The XML-Layout

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:center_latitude="51"
    mapbox:center_longitude="7"
    mapbox:style_url="http://demo.tileserver.org/styles/klokantech-basic.json"
    mapbox:zoom="1"/>

The code for downloading the map data:

// Set up the OfflineManager
OfflineManager offlineManager = OfflineManager.getInstance(context);

// Create a bounding box for the offline region
LatLngBounds latLngBounds = new LatLngBounds.Builder()
        .include(new LatLng(6, 50))
        .include(new LatLng(8, 52))
        .build();

// Define the offline region
OfflineTilePyramidRegionDefinition definition = new OfflineTilePyramidRegionDefinition(
        mapView.getStyleUrl(),
        latLngBounds,
        0,
        9, // also tried other zoom levels
        context.getResources().getDisplayMetrics().density);

// Set the metadata
byte[] metadata;
try {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(JSON_FIELD_REGION_NAME, "Cologne");
    String json = jsonObject.toString();
    metadata = json.getBytes(JSON_CHARSET);
} catch (Exception exception) {
    Log.e("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);

                // 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
                            Log.d("Region downloaded successfully.");
                            ReadOSRMRouteTask readOSRMRouteTask = new ReadOSRMRouteTask();
                            readOSRMRouteTask.execute();
                        } else if (status.isRequiredResourceCountPrecise()) {
                            // Switch to determinate state
                            Log.d((int) Math.round(percentage) + "% downloaded");
                        }
                    }

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

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

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

While downloading the map data, the log basically just spammed a lot of HTTP 200s, so everything seems fine on this end. Additionally, the offline packages are reported complete and the sqlite-db seems fine as well.

When starting the app in offline mode, this is basically the log:

D/mbgl: [JNI]: nativeCreate

/com.mapbox.mapboxsdk.maps.MapView: MapView start Telemetry...

/MapboxEventManager: Telemetry initialize() called...

/MapboxEventManager: Mapbox Telemetry has already been initialized.

D/mbgl: [JNI]: nativeInitializeDisplay

D/mbgl: [JNI]: nativeInitializeContext

I/MapboxEventManager: flushEventsQueueImmediately() called...

D/MapboxEventManager: turnstile event pushed.

W/MapboxEventManager: Not connected to network, so empty events cache and return without attempting to send events

I/com.mapbox.mapboxsdk.http.HTTPRequest: Request failed due to a connection error: No Internet connection available.

D/mbgl: [JNI]: nativeViewResize

D/mbgl: [JNI]: nativeCreateSurface

D/mbgl: [JNI]: nativeFramebufferResize

I/TelemetryService: onStartCommand() called

D/mbgl: [JNI]: nativeViewResize

D/mbgl: [JNI]: nativeFramebufferResize

I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@41bd28b8 time:609768

W/MapboxEventManager: Not connected to network, so empty events cache and return without attempting to send events

1
I think I had this issue too and will look back through the project, but I think you need to make sure you are downloading the same style in your offline regions.dazza5000
Thanks for checking! Shouldn't 'mapView.getStyleUrl()' already make sure, that the same style is downloaded, as it's already being used? I'm not changing the map style after setting it in the layout-XML.Phil

1 Answers

0
votes

Can you provide more information on the issue such as any log output and the behavior this is happening vs what you expected? Make sure you are using the same mapbox style URL for both the offline download and your mapviews style.