Android Open Source - hellomap3d-android Vector Map Sample Base Activity






From Project

Back to project page hellomap3d-android.

License

The source code is released under:

MIT License

If you think the Android project hellomap3d-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nutiteq.advancedmap3;
/*from   w  w w  . j  ava  2 s  . c  o m*/
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;

import com.nutiteq.core.MapRange;
import com.nutiteq.datasources.CompressedCacheTileDataSource;
import com.nutiteq.datasources.NutiteqOnlineTileDataSource;
import com.nutiteq.datasources.PersistentCacheTileDataSource;
import com.nutiteq.datasources.TileDataSource;
import com.nutiteq.layers.VectorTileLayer;
import com.nutiteq.utils.AssetUtils;
import com.nutiteq.vectortiles.MBVectorTileDecoder;
import com.nutiteq.vectortiles.MBVectorTileStyleSet;
import com.nutiteq.wrappedcommons.UnsignedCharVector;

/**
 * Base activity for vector map samples. Adds menu with multiple style choices.
 * 
 * @author jaak
 *
 */
public class VectorMapSampleBaseActivity extends MapSampleBaseActivity {

    protected MBVectorTileDecoder vectorTileDecoder;
    protected boolean persistentTileCache = false;
    
    // Style parameters
    protected String vectorStyleName = "osmbright"; // default style name, each style has corresponding .zip asset
    protected String vectorStyleLang = "en"; // default map language

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

        // Update options
        mapView.getOptions().setZoomRange(new MapRange(0, 20));
        
        // Set default base map - online vector with persistent caching
        updateBaseLayer();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
      Menu langMenu = menu.addSubMenu("Language");
      addLanguageMenuOption(langMenu, "English", "en");
      addLanguageMenuOption(langMenu, "German",  "de");
      addLanguageMenuOption(langMenu, "French",  "fr");
      addLanguageMenuOption(langMenu, "Russian", "ru");
      addLanguageMenuOption(langMenu, "Chinese", "zh");
     
      Menu styleMenu = menu.addSubMenu("Style");
      addStyleMenuOption(styleMenu, "Basic", "basic");
      addStyleMenuOption(styleMenu, "OSM Bright 2D", "osmbright");
      addStyleMenuOption(styleMenu, "OSM Bright 3D", "osmbright3d");
      addStyleMenuOption(styleMenu, "OSM Bright Chinese", "osmbright-unifont");
      addStyleMenuOption(styleMenu, "Loose Leaf", "looseleaf");

      return true;
    }
    
    private void addLanguageMenuOption(final Menu menu, String text, final String value) {
      MenuItem menuItem = menu.add(text).setOnMenuItemClickListener(new OnMenuItemClickListener(){
          @Override
          public boolean onMenuItemClick (MenuItem item){
            for (int i = 0; i < menu.size(); i++) {
              MenuItem otherItem = menu.getItem(i);
              if (otherItem == item) {
                otherItem.setIcon(android.R.drawable.checkbox_on_background);
              } else {
                otherItem.setIcon(null);
              }
            }
            vectorStyleLang = value;
            updateBaseLayer();
              return true;
          }
      });
      if (vectorStyleLang.equals(value)) {
        menuItem.setIcon(android.R.drawable.checkbox_on_background);
      }
    }
    
    private void addStyleMenuOption(final Menu menu, String text, final String value) {
      MenuItem menuItem = menu.add(text).setOnMenuItemClickListener(new OnMenuItemClickListener(){
          @Override
          public boolean onMenuItemClick (MenuItem item){
            for (int i = 0; i < menu.size(); i++) {
              MenuItem otherItem = menu.getItem(i);
              if (otherItem == item) {
                otherItem.setIcon(android.R.drawable.checkbox_on_background);
              } else {
                otherItem.setIcon(null);
              }
            }
            vectorStyleName = value;
            updateBaseLayer();
              return true;
          }
      });
      if (vectorStyleName.equals(value)) {
        menuItem.setIcon(android.R.drawable.checkbox_on_background);
      }      
    }
    
    private void updateBaseLayer() {
      String styleAssetName = vectorStyleName + ".zip";
      boolean styleBuildings3D = false;
      if (vectorStyleName.equals("osmbright3d")) {
        styleAssetName = "osmbright.zip";
        styleBuildings3D = true;
      }
        UnsignedCharVector styleBytes = AssetUtils.loadBytes(styleAssetName);
        if (styleBytes != null){
          // Create style set
            MBVectorTileStyleSet vectorTileStyleSet = new MBVectorTileStyleSet(styleBytes);
            vectorTileDecoder = new MBVectorTileDecoder(vectorTileStyleSet);
            
            // Set language, language-specific texts from vector tiles will be used
            vectorTileDecoder.setStyleParameter("lang", vectorStyleLang);
            
            // OSM Bright style set supports choosing between 2d/3d buildings. Set corresponding parameter.
            if (styleAssetName.equals("osmbright.zip")) {
              vectorTileDecoder.setStyleParameter("buildings3d", styleBuildings3D);
            }
            
            // Create tile data source for vector tiles
            TileDataSource vectorTileDataSource = createTileDataSource();

            // Remove old base layer, create new base layer
            if (baseLayer != null) {
              mapView.getLayers().remove(baseLayer);
            }
            baseLayer = new VectorTileLayer(vectorTileDataSource, vectorTileDecoder);
            mapView.getLayers().add(baseLayer);
        } else {
            Log.e(Const.LOG_TAG, "map style file must be in project assets: "+vectorStyleName);          
        }
    }
    
    protected TileDataSource createTileDataSource() {
        TileDataSource vectorTileDataSource = new NutiteqOnlineTileDataSource("nutiteq.mbstreets");

        // We don't use vectorTileDataSource directly (this would be also option),
        // but via caching to cache data locally persistently/non-persistently
        // Note that persistent cache requires WRITE_EXTERNAL_STORAGE permission
        TileDataSource cacheDataSource;
        if (persistentTileCache) {
          String cacheFile = getExternalFilesDir(null)+"/mapcache.db";
          Log.i(Const.LOG_TAG,"cacheFile = "+cacheFile);
          cacheDataSource = new PersistentCacheTileDataSource(vectorTileDataSource, cacheFile);
        } else {
          cacheDataSource = new CompressedCacheTileDataSource(vectorTileDataSource);
        }
      return cacheDataSource;
    }
}




Java Source Code List

com.nutiteq.advancedmap3.AerialMapActivity.java
com.nutiteq.advancedmap3.AnimatedRasterMapActivity.java
com.nutiteq.advancedmap3.Const.java
com.nutiteq.advancedmap3.CustomRasterDataSourceActivity.java
com.nutiteq.advancedmap3.GraphhopperRouteActivity.java
com.nutiteq.advancedmap3.LauncherList.java
com.nutiteq.advancedmap3.MapListenerActivity.java
com.nutiteq.advancedmap3.MapSampleBaseActivity.java
com.nutiteq.advancedmap3.MbtilesActivity.java
com.nutiteq.advancedmap3.OfflineVectorMapActivity.java
com.nutiteq.advancedmap3.Overlays2DActivity.java
com.nutiteq.advancedmap3.Overlays3DActivity.java
com.nutiteq.advancedmap3.PinMapActivity.java
com.nutiteq.advancedmap3.RasterOverlayActivity.java
com.nutiteq.advancedmap3.VectorMapSampleBaseActivity.java
com.nutiteq.advancedmap3.datasource.MyAnimatedTileDataSource.java
com.nutiteq.advancedmap3.datasource.MyMergedRasterTileDataSource.java
com.nutiteq.advancedmap3.listener.MyMapEventListener.java
com.nutiteq.advancedmap3.listener.RouteMapEventListener.java
com.nutiteq.filepicker.FilePickerActivity.java
com.nutiteq.filepicker.FilePickerIconAdapter.java
com.nutiteq.filepicker.FilePicker.java
com.nutiteq.hellomap3.MainActivity.java
com.nutiteq.hellomap3.util.AssetCopy.java