[android] #2586 - Refactor Style with Android Support Annotations StringDef

This commit is contained in:
Tobrun
2015-10-11 01:35:14 +02:00
parent dc98966e6d
commit 3039341653
6 changed files with 60 additions and 47 deletions

View File

@@ -0,0 +1,36 @@
package com.mapbox.mapboxsdk.constants;
import android.support.annotation.StringDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class Style {
@StringDef({MAPBOX_STREETS, EMERALD, LIGHT, DARK, SATELLITE})
@Retention(RetentionPolicy.SOURCE)
public @interface StyleUrl {
}
/**
* Mapbox Streets: Our signature style.
*/
public static final String MAPBOX_STREETS = "asset://styles/streets-v8.json";
/**
* Emerald: Great for transportation and outdoor terrain.
*/
public static final String EMERALD = "asset://styles/emerald-v8.json";
/**
* Light: Light-colored style that is great for data overlay.
*/
public static final String LIGHT = "asset://styles/light-v8.json";
/**
* Dark: Dark-colored style that is great for data overlay.
*/
public static final String DARK = "asset://styles/dark-v8.json";
/**
* Satellite: The best-looking, most accurate, and most up-to-date satellite imagery available anywhere.
*/
public static final String SATELLITE = "asset://styles/satellite-v8.json";
}

View File

@@ -61,6 +61,7 @@ import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
import com.mapbox.mapboxsdk.geometry.BoundingBox;
import com.mapbox.mapboxsdk.geometry.LatLng;
@@ -243,41 +244,6 @@ public final class MapView extends FrameLayout {
// Inner classes
//
// These provide easy access to the bundled styles
/**
* StyleUrls provides URLs to several professional styles designed by Mapbox.
* <p/>
* These styles are all ready to go in your app. To load one, pass it into {@link MapView#setStyleUrl(String)}
*
* @see MapView#setStyleUrl(String)
*/
public static final class StyleUrls {
private StyleUrls() {
}
/**
* Mapbox Streets: Our signature style.
*/
public static final String MAPBOX_STREETS = "asset://styles/streets-v8.json";
/**
* Emerald: Great for transportation and outdoor terrain.
*/
public static final String EMERALD = "asset://styles/emerald-v8.json";
/**
* Light: Light-colored style that is great for data overlay.
*/
public static final String LIGHT = "asset://styles/light-v8.json";
/**
* Dark: Dark-colored style that is great for data overlay.
*/
public static final String DARK = "asset://styles/dark-v8.json";
/**
* Satellite: The best-looking, most accurate, and most up-to-date satellite imagery available anywhere.
*/
public static final String SATELLITE = "asset://styles/satellite-v8.json";
}
//
// Enums
//
@@ -1335,7 +1301,7 @@ public final class MapView extends FrameLayout {
* <p/>
* {@code url} can take the following forms:
* <ul>
* <li>{@code MapView.StyleUrls.*}: load one of the bundled styles in {@link MapView.StyleUrls}.</li>
* <li>{@code MapView.StyleUrls.*}: load one of the bundled styles in {@link Style}.</li>
* <li>{@code mapbox://styles/<user>/<style>}:
* retrieves the style from a <a href="https://www.mapbox.com/account/">Mapbox account.</a>
* {@code user} is your username. {@code style} is the ID of your custom
@@ -1345,7 +1311,7 @@ public final class MapView extends FrameLayout {
* <li>{@code asset://...}:
* reads the style from the APK {@code asset/} directory.
* This is used to load a style bundled with your app.</li>
* <li>{@code null}: loads the default {@link MapView.StyleUrls#MAPBOX_STREETS} style.</li>
* <li>{@code null}: loads the default {@link Style#MAPBOX_STREETS} style.</li>
* </ul>
* <p/>
* This method is asynchronous and will return immediately before the style finishes loading.
@@ -1355,17 +1321,23 @@ public final class MapView extends FrameLayout {
* An error message will be logged in the Android logcat and {@link MapView.MapChange#DidFailLoadingMap} event will be sent.
*
* @param url The URL of the map style
* @see MapView.StyleUrls
* @see Style
*/
@UiThread
public void setStyleUrl(@Nullable String url) {
if (url == null) {
url = StyleUrls.MAPBOX_STREETS;
url = Style.MAPBOX_STREETS;
}
mStyleUrl = url;
mNativeMapView.setStyleUrl(url);
}
@UiThread
@NonNull
public void setStyle(@Style.StyleUrl String style){
setStyleUrl(style);
}
/**
* Returns the map style currently displayed in the map view.
* <p/>

View File

@@ -11,6 +11,7 @@ import android.widget.TextView;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
@@ -38,7 +39,7 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
mMapView = (MapView) findViewById(R.id.secondMapView);
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.onCreate(savedInstanceState);
mMapView.setStyleUrl(MapView.StyleUrls.MAPBOX_STREETS);
mMapView.setStyle(Style.MAPBOX_STREETS);
mMapView.setInfoWindowAdapter(new MapView.InfoWindowAdapter() {
private int tenDp = (int) getResources().getDimension(R.dimen.attr_margin);

View File

@@ -28,6 +28,7 @@ import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
@@ -160,6 +161,7 @@ public class MainActivity extends AppCompatActivity {
mSelectedStyle = savedInstanceState.getInt(STATE_SELECTED_STYLE);
}
// Set default UI state
mNavigationView.getMenu().findItem(R.id.action_compass).setChecked(mMapView.isCompassEnabled());
mNavigationView.getMenu().findItem(R.id.action_debug).setChecked(mMapView.isDebugActive());
@@ -325,27 +327,27 @@ public class MainActivity extends AppCompatActivity {
private boolean changeMapStyle(int id) {
switch (id) {
case R.id.actionStyleMapboxStreets:
mMapView.setStyleUrl(MapView.StyleUrls.MAPBOX_STREETS);
mMapView.setStyle(Style.MAPBOX_STREETS);
mSelectedStyle = id;
return true;
case R.id.actionStyleEmerald:
mMapView.setStyleUrl(MapView.StyleUrls.EMERALD);
mMapView.setStyle(Style.EMERALD);
mSelectedStyle = id;
return true;
case R.id.actionStyleLight:
mMapView.setStyleUrl(MapView.StyleUrls.LIGHT);
mMapView.setStyle(Style.LIGHT);
mSelectedStyle = id;
return true;
case R.id.actionStyleDark:
mMapView.setStyleUrl(MapView.StyleUrls.DARK);
mMapView.setStyle(Style.DARK);
mSelectedStyle = id;
return true;
case R.id.actionStyleSatellite:
mMapView.setStyleUrl(MapView.StyleUrls.SATELLITE);
mMapView.setStyle(Style.SATELLITE);
mSelectedStyle = id;
return true;

View File

@@ -10,6 +10,7 @@ import android.view.MenuItem;
import android.view.View;
import com.mapbox.mapboxsdk.MapFragment;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;
@@ -48,7 +49,7 @@ public class MapFragmentActivity extends AppCompatActivity {
super.onViewCreated(view, savedInstanceState);
MapView mapView = getMap();
mapView.setStyleUrl(MapView.StyleUrls.EMERALD);
mapView.setStyleUrl(Style.EMERALD);
mapView.setZoomLevel(12);
mapView.setCenterCoordinate(new LatLng(50.853658, 4.352419));

View File

@@ -11,6 +11,7 @@ import android.view.MotionEvent;
import android.view.View;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
@@ -40,7 +41,7 @@ public class PressForMarkerActivity extends AppCompatActivity {
mMapView = (MapView) findViewById(R.id.pressForMarkerMapView);
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.setStyleUrl(MapView.StyleUrls.EMERALD);
mMapView.setStyleUrl(Style.EMERALD);
mMapView.onCreate(savedInstanceState);
mMapView.setCenterCoordinate(new LatLng(45.1855569, 5.7215506));