Add dropdown to select map style

This commit is contained in:
Leith Bade
2014-11-23 16:06:50 +11:00
parent 8c5be67f14
commit 198a8eb455
8 changed files with 134 additions and 45 deletions

View File

@@ -1,8 +1,14 @@
package com.mapbox.mapboxgl.app;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.mapbox.mapboxgl.lib.MapView;
@@ -15,6 +21,13 @@ public class MainActivity extends ActionBarActivity {
// Tag used for logging
private static final String TAG = "MainActivity";
//
// Instance members
//
// Holds the MapFragment
MapFragment mMapFragment;
//
// Lifecycle events
//
@@ -25,10 +38,65 @@ public class MainActivity extends ActionBarActivity {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
// Show the app icon
//getActionBar().setHomeButtonEnabled(true);
// Load the layout
setContentView(R.layout.activity_main);
mMapFragment = (MapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
// TODO move to layout file and save state
// Add the spinner to select map styles
Spinner styleSpinner = (Spinner)findViewById(R.id.spinner_style);
ArrayAdapter styleAdapter = ArrayAdapter.createFromResource(this,
R.array.style_list, android.R.layout.simple_spinner_dropdown_item);
styleSpinner.setAdapter(styleAdapter);
styleSpinner.setOnItemSelectedListener(new StyleSpinnerListener());
// Add a toolbar as the action bar
Toolbar mainToolbar = (Toolbar)findViewById(R.id.toolbar_main);
setSupportActionBar(mainToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
// This class handles navigation events
private class StyleSpinnerListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
// Basic
case 0:
mMapFragment.getMap().setStyleUrl("asset://styles/styles/basic-v6.json");
break;
// Bright
case 1:
mMapFragment.getMap().setStyleUrl("asset://styles/styles/bright-v6.json");
break;
// Outdoors (Vector)
case 2:
mMapFragment.getMap().setStyleUrl("asset://styles/styles/outdoors-v6.json");
break;
// Outdoors (Raster)
case 3:
mMapFragment.getMap().setStyleUrl("asset://styles/styles/outdoors-raster-v6.json");
break;
// Pencil
case 4:
mMapFragment.getMap().setStyleUrl("asset://styles/styles/pencil-v6.json");
break;
// Satellite
case 5:
mMapFragment.getMap().setStyleUrl("asset://styles/styles/satellite-v6.json");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
mMapFragment.getMap().setStyleUrl("");
}
}
}

View File

@@ -7,7 +7,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxgl.lib.LonLat;
import com.mapbox.mapboxgl.lib.MapView;
public class MapFragment extends Fragment {
@@ -19,8 +18,12 @@ public class MapFragment extends Fragment {
// Tag used for logging
private static final String TAG = "MapFragment";
//
// Instance members
//
// The map
private MapView mMapView;
private MapView mMap;
//
// Lifecycle events
@@ -33,16 +36,16 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onCreateView");
// Create the map
mMapView = (MapView) inflater.inflate(R.layout.fragment_main, container, true);
mMap = (MapView) inflater.inflate(R.layout.fragment_main, container, true);
// Load the API key
mMapView.setAccessToken("pk.eyJ1IjoibGpiYWRlIiwiYSI6IlJSQ0FEZ2MifQ.7mE4aOegldh3595AG9dxpQ");
mMap.setAccessToken("pk.eyJ1IjoibGpiYWRlIiwiYSI6IlJSQ0FEZ2MifQ.7mE4aOegldh3595AG9dxpQ");
// Need to pass on any saved state to the map
mMapView.onCreate(savedInstanceState);
mMap.onCreate(savedInstanceState);
// Return the map as the root view
return mMapView;
return mMap;
}
// Called when the fragment is destroyed
@@ -52,8 +55,8 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onDestroyView");
// Need to pass on to view
mMapView.onDestroy();
mMapView = null;
mMap.onDestroy();
mMap = null;
}
// Called when the fragment is visible
@@ -63,7 +66,7 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onStart");
// Need to pass on to view
mMapView.onStart();
mMap.onStart();
}
// Called when the fragment is invisible
@@ -73,7 +76,7 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onStop");
// Need to pass on to view
mMapView.onStop();
mMap.onStop();
}
// Called when the fragment is in the background
@@ -83,7 +86,7 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onPause");
// Need to pass on to view
mMapView.onPause();
mMap.onPause();
}
// Called when the fragment is no longer in the background
@@ -93,7 +96,7 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onResume");
// Need to pass on to view
mMapView.onResume();
mMap.onResume();
}
// Called before fragment is destroyed
@@ -102,7 +105,7 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onSaveInstanceState");
// Need to retrieve any saved state from the map
mMapView.onSaveInstanceState(outState);
mMap.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@@ -110,7 +113,7 @@ public class MapFragment extends Fragment {
// Property methods
//
public MapView getMapView() {
return mMapView;
public MapView getMap() {
return mMap;
}
}

View File

@@ -1,7 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
tools:context="${packageName}.${activityClass}"
android:name="com.mapbox.mapboxgl.app.MapFragment"
android:id="@+id/map_fragment"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">
<Spinner
android:id="@+id/spinner_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
tools:context="${packageName}.${activityClass}"
android:name="com.mapbox.mapboxgl.app.MapFragment"
android:id="@+id/fragment_map"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>

View File

@@ -2,5 +2,4 @@
<com.mapbox.mapboxgl.lib.MapView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:styleUrl="asset://styles/styles/bright-v6.json" />
android:layout_height="match_parent" />

View File

@@ -3,4 +3,13 @@
<string name="app_name">Mapbox GL</string>
<string-array name="style_list">
<item>Basic</item>
<item>Bright</item>
<item>Outdoors (Vector)</item>
<item>Outdoors (Raster)</item>
<item>Pencil</item>
<item>Satellite</item>
</string-array>
</resources>

View File

@@ -1,20 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" />
<style name="AppTheme" parent="AppBaseTheme" />
</resources>

View File

@@ -60,7 +60,6 @@ public class MapView extends SurfaceView {
private static final String STATE_APPLIED_CLASSES = "appliedClasses";
private static final String STATE_DEFAULT_TRANSITION_DURATION = "defaultTransitionDuration";
//
// Instance members
//
@@ -161,7 +160,9 @@ public class MapView extends SurfaceView {
setScrollEnabled(typedArray.getBoolean(R.styleable.MapView_scrollEnabled, true));
setRotateEnabled(typedArray.getBoolean(R.styleable.MapView_rotateEnabled, true));
setDebugActive(typedArray.getBoolean(R.styleable.MapView_debugActive, false));
setStyleUrl(typedArray.getString(R.styleable.MapView_styleUrl));
if (typedArray.getString(R.styleable.MapView_styleUrl) != null) {
setStyleUrl(typedArray.getString(R.styleable.MapView_styleUrl));
}
} finally {
typedArray.recycle();
}