1

So I'm trying to use the Mapbox SDK in an offline mode by merging tiles in a local DB file I created from my own tile server by using

./mbgl-offline --north [north coordinate] --west [west coordinate] --south [south coordinate] --east [east coordinate] --minZoom [minimum zoom] --maxZoom [maximum zoom] --output [output file path] --style [style URL] --token [mapbox token]

command from the mapbox-gl-native library.

I put the file in the /path/to/file/files directory and I implemented the merging code as shown in the example : merge offline tiles example

When I open the app I get a toast says the merging went successfully but I can't see the map. all I see is a blank surface with the background color of my map style.

As shown in the example, I make sure the app is not connected to the internet and then I merge the DB file before I load the style:

    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 map view.
    Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
    setContentView(R.layout.activity_main);

    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    Mapbox.setConnected(false);
    mergeDb();
    mapView.getMapAsync(this);
}

@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
    MainActivity.this.mapboxMap = mapboxMap;

    mapboxMap.setStyle( new Style.Builder().fromUrl("http://<my_server_ip>/styles/klokantech-basic/style.json")
      , new Style.OnStyleLoaded() {
          @Override
          public void onStyleLoaded(@NonNull Style style) {

//                  enableLocationComponent(style);

          }
      });
}

My mergeDb function:

    private void mergeDb(){
    System.out.println(FileSource.getResourcesCachePath(this));
    OfflineManager.getInstance(this).mergeOfflineRegions(FileSource.getResourcesCachePath(this) + "/myfile.db", new OfflineManager.MergeOfflineRegionsCallback() {
        @Override
        public void onMerge(OfflineRegion[] offlineRegions) {
            mapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(@NonNull MapboxMap mapboxMap) {
                    mapboxMap.setStyle(new Style.Builder().fromUrl("http://<my_server_ip>/styles/klokantech-basic/style.json"));

                }
            });

            Toast.makeText(
              MainActivity.this,
              String.format("Merged %d regions.", offlineRegions.length),
              Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(String error) {

            Toast.makeText(
              MainActivity.this,
            String.format("Offline DB merge error." ),
              Toast.LENGTH_LONG).show();
            System.out.println(error);

        }
    });
}

Any idea what could be the problem?

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
kitsuneFox
  • 1,243
  • 3
  • 18
  • 31
  • you are not connected to internet but trying to call `"http:///styles/klokantech-basic/style.json"`. How does this sound to you? – Rahul Mar 12 '19 at 09:54
  • You are welcome to go to the mapbox example in the question and see that they also did this by referring to STYLE.SATELLITE which is also an address and by the way, I tried it but no success either. How does this sound to you? – kitsuneFox Mar 12 '19 at 14:11
  • Does your `style.json` point to a style with valid sources or include any styling? A successful merge without those two things properly in place would likely create the outcome you are describing. – riastrad Apr 29 '19 at 20:41
  • The style URL you use in the app must be the same as what's in the offline pack, if you're not using mapbox:/// then I'm not sure if this is guaranteed to work. I'd suggest opening the offline db and seeing what's the style resource it uses. You could even try changing both in the offline pack and your Java code to mapbox:/// to see if that works... – AndrewHarvey Sep 17 '19 at 09:26
  • @kitsuneFox, did you manage to solve this? – sedot Aug 30 '21 at 22:05

0 Answers0