/*
 * Copyright 2017 Nextome S.r.l
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nextome.geojsonviewer;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Toast;

import org.osmdroid.bonuspack.kml.KmlDocument;
import org.osmdroid.bonuspack.kml.Style;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.FolderOverlay;


public class OpenStreetMapActivity extends MapBaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_street_map);
        this.getIntentExtras(getIntent());

        final MapView map = (MapView) findViewById(R.id.map);
        map.setTileSource(TileSourceFactory.MAPNIK);
        map.setMultiTouchControls(true);
        map.getController().setZoom(4);
        map.setMaxZoomLevel(null);

        final KmlDocument kmlDocument = new KmlDocument();

        try {
            for (int i=0; i<this.getJsonUris().size(); i++) {
                Uri uri = this.getJsonUris().get(i);
                kmlDocument.parseGeoJSON(FileUtilities.getStringFromFile(uri, this.getContext()));

                Drawable defaultMarker = getResources().getDrawable(R.drawable.marker_default);
                Bitmap defaultBitmap = ((BitmapDrawable) defaultMarker).getBitmap();
                Style defaultStyle = new Style(defaultBitmap, this.getJsonColors().get(i), 2f, 0x00000000);
                FolderOverlay geoJsonOverlay = (FolderOverlay) kmlDocument.mKmlRoot.buildOverlay(map, defaultStyle, null, kmlDocument);

                map.getOverlays().add(geoJsonOverlay);
                map.invalidate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this.getContext(), R.string.geojson_opener_unable_to_read, Toast.LENGTH_LONG).show();
        }


        // Workaround for osmdroid issue
        // See: https://github.com/osmdroid/osmdroid/issues/337
        map.addOnFirstLayoutListener(new MapView.OnFirstLayoutListener() {
            @Override
            public void onFirstLayout(View v, int left, int top, int right, int bottom) {
                BoundingBox boundingBox = kmlDocument.mKmlRoot.getBoundingBox();
                map.zoomToBoundingBox(boundingBox, false);
            }
        });
    }



    @Override
    public void onResume() {
        super.onResume();
        Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
    }

}

@@ -3132,49 +3132,324 @@
     
-map.zoomToBoundingBox(boundingBox, false)
+// Yep it's called 2 times. Another workaround for zoomToBoundingBox.%0A                // See: https://github.com/osmdroid/osmdroid/issues/236#issuecomment-257061630%0A                map.zoomToBoundingBox(boundingBox, false);%0A                map.zoomToBoundingBox(boundingBox, false);%0A                map.invalidate();
 ;%0A  

Fix issue with zoomToBoundingBox in OpenStreetMaps.