[android] - add example of camera zoom function on a symbol layer.
This commit is contained in:
@@ -626,12 +626,12 @@
|
||||
android:value=".activity.FeatureOverviewActivity"/>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activity.annotation.AddRemoveMarkerActivity"
|
||||
android:name=".activity.style.ZoomFunctionSymbolLayerActivity"
|
||||
android:description="@string/description_add_remove_markers"
|
||||
android:label="@string/activity_add_remove_markers">
|
||||
<meta-data
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_annotation"/>
|
||||
android:value="@string/category_style"/>
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value=".activity.FeatureOverviewActivity"/>
|
||||
|
||||
@@ -1,188 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.annotation;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.utils.ViewToBitmapUtil;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* Test activity showcasing updating a Marker image when changing zoom levels
|
||||
*/
|
||||
public class AddRemoveMarkerActivity extends AppCompatActivity {
|
||||
|
||||
public static final double THRESHOLD = 5.0;
|
||||
|
||||
private MapView mapView;
|
||||
private MapboxMap mapboxMap;
|
||||
private double lastZoom;
|
||||
private boolean isShowingHighThresholdMarker;
|
||||
private boolean isShowingLowThresholdMarker;
|
||||
|
||||
private MarkerOptions lowThresholdMarker;
|
||||
private MarkerOptions highThresholdMarker;
|
||||
private Marker activeMarker;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_add_remove_marker);
|
||||
|
||||
View lowThresholdView = generateView("Low", R.drawable.ic_circle);
|
||||
Bitmap lowThresholdBitmap = ViewToBitmapUtil.convertToBitmap(lowThresholdView);
|
||||
Icon lowThresholdIcon = IconFactory.getInstance(this).fromBitmap(lowThresholdBitmap);
|
||||
|
||||
lowThresholdMarker = new MarkerOptions()
|
||||
.icon(lowThresholdIcon)
|
||||
.position(new LatLng(0.1, 0));
|
||||
|
||||
View highThesholdView = generateView("High", R.drawable.ic_circle);
|
||||
Bitmap highThresholdBitmap = ViewToBitmapUtil.convertToBitmap(highThesholdView);
|
||||
Icon highThresholdIcon = IconFactory.getInstance(this).fromBitmap(highThresholdBitmap);
|
||||
|
||||
highThresholdMarker = new MarkerOptions()
|
||||
.icon(highThresholdIcon)
|
||||
.position(new LatLng(0.1, 0));
|
||||
|
||||
mapView = (MapView) findViewById(R.id.mapView);
|
||||
mapView.onCreate(savedInstanceState);
|
||||
mapView.getMapAsync(new OnMapReadyCallback() {
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
AddRemoveMarkerActivity.this.mapboxMap = mapboxMap;
|
||||
updateZoom(mapboxMap.getCameraPosition().zoom);
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(4.9f));
|
||||
mapboxMap.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() {
|
||||
@Override
|
||||
public void onCameraChange(CameraPosition position) {
|
||||
updateZoom(position.zoom);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
private View generateView(String text, @DrawableRes int drawableRes) {
|
||||
View view = LayoutInflater.from(this).inflate(R.layout.view_custom_marker, null);
|
||||
TextView textView = (TextView) view.findViewById(R.id.textView);
|
||||
textView.setText(text);
|
||||
textView.setTextColor(Color.WHITE);
|
||||
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
|
||||
imageView.setImageResource(drawableRes);
|
||||
return view;
|
||||
}
|
||||
|
||||
private void updateZoom(double zoom) {
|
||||
if (lastZoom == zoom) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastZoom = zoom;
|
||||
if (zoom > THRESHOLD) {
|
||||
showHighThresholdMarker();
|
||||
} else {
|
||||
showLowThresholdMarker();
|
||||
}
|
||||
}
|
||||
|
||||
private void showLowThresholdMarker() {
|
||||
if (isShowingLowThresholdMarker) {
|
||||
return;
|
||||
}
|
||||
|
||||
isShowingLowThresholdMarker = true;
|
||||
isShowingHighThresholdMarker = false;
|
||||
|
||||
if (activeMarker != null) {
|
||||
Timber.d("Remove marker with id %s", activeMarker.getId());
|
||||
mapboxMap.removeMarker(activeMarker);
|
||||
} else {
|
||||
Timber.e("active marker is null");
|
||||
}
|
||||
|
||||
activeMarker = mapboxMap.addMarker(lowThresholdMarker);
|
||||
Timber.d("showLowThresholdMarker() with id %s ", activeMarker.getId());
|
||||
}
|
||||
|
||||
private void showHighThresholdMarker() {
|
||||
if (isShowingHighThresholdMarker) {
|
||||
return;
|
||||
}
|
||||
|
||||
isShowingLowThresholdMarker = false;
|
||||
isShowingHighThresholdMarker = true;
|
||||
|
||||
if (activeMarker != null) {
|
||||
Timber.d("Remove marker with id %s", activeMarker.getId());
|
||||
mapboxMap.removeMarker(activeMarker);
|
||||
} else {
|
||||
Timber.e("active marker is null");
|
||||
}
|
||||
|
||||
activeMarker = mapboxMap.addMarker(highThresholdMarker);
|
||||
Timber.d("showHighThresholdMarker() with id %s", activeMarker.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
mapView.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
mapView.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
mapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
mapView.onLowMemory();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mapView.onDestroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.style;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
|
||||
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
import static com.mapbox.mapboxsdk.style.functions.Function.zoom;
|
||||
import static com.mapbox.mapboxsdk.style.functions.stops.Stop.stop;
|
||||
import static com.mapbox.mapboxsdk.style.functions.stops.Stops.interval;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
|
||||
|
||||
public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {
|
||||
|
||||
private static final String SOURCE_URL = "https://gist.githubusercontent.com/anonymous/"
|
||||
+ "70ddad0e58520307cd1699e704f4b626/raw/a5deaccdc4517c12d63b1f8abcb7becf5e36506a/map.geojson";
|
||||
private static final String LAYER_ID = "symbolLayer";
|
||||
private static final String SOURCE_ID = "poiSource";
|
||||
private static final String BUS_MAKI_ICON_ID = "bus-11";
|
||||
private static final String CAFE_MAKI_ICON_ID = "cafe-11";
|
||||
private static final float ZOOM_STOP_MIN_VALUE = 7.0f;
|
||||
private static final float ZOOM_STOP_MAX_VALUE = 12.0f;
|
||||
|
||||
private MapView mapView;
|
||||
private MapboxMap mapboxMap;
|
||||
private GeoJsonSource source;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_zoom_symbol_layer);
|
||||
|
||||
mapView = (MapView) findViewById(R.id.mapView);
|
||||
mapView.onCreate(savedInstanceState);
|
||||
mapView.getMapAsync(new OnMapReadyCallback() {
|
||||
@Override
|
||||
public void onMapReady(@NonNull final MapboxMap map) {
|
||||
mapboxMap = map;
|
||||
addSource();
|
||||
addLayer();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void addSource() {
|
||||
try {
|
||||
source = new GeoJsonSource(SOURCE_ID, new URL(SOURCE_URL));
|
||||
} catch (MalformedURLException exception) {
|
||||
Timber.e(exception, "That's not an url... ");
|
||||
}
|
||||
mapboxMap.addSource(source);
|
||||
}
|
||||
|
||||
private void addLayer() {
|
||||
SymbolLayer layer = new SymbolLayer(LAYER_ID, SOURCE_ID);
|
||||
layer.setProperties(
|
||||
iconImage(
|
||||
zoom(
|
||||
interval(
|
||||
stop(ZOOM_STOP_MIN_VALUE, iconImage(BUS_MAKI_ICON_ID)),
|
||||
stop(ZOOM_STOP_MAX_VALUE, iconImage(CAFE_MAKI_ICON_ID))
|
||||
)
|
||||
)
|
||||
),
|
||||
iconAllowOverlap(true)
|
||||
);
|
||||
mapboxMap.addLayer(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
mapView.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
mapView.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
mapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
mapView.onLowMemory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
mapView.onDestroy();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="oval">
|
||||
|
||||
<solid
|
||||
android:color="@color/mapbox_blue"/>
|
||||
|
||||
<size
|
||||
android:width="@dimen/circle_size"
|
||||
android:height="@dimen/circle_size"/>
|
||||
</shape>
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@+id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:mapbox_cameraTargetLat="40.730648"
|
||||
app:mapbox_cameraTargetLng="-73.993619"
|
||||
app:mapbox_cameraZoom="11"
|
||||
app:mapbox_styleUrl="@string/mapbox_style_light"/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<dimen name="circle_size">24dp</dimen>
|
||||
<dimen name="fab_margin">16dp</dimen>
|
||||
<dimen name="attr_margin">10dp</dimen>
|
||||
<dimen name="map_padding_left">96dp</dimen>
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<string name="activity_polygon">Polygon</string>
|
||||
<string name="activity_press_for_marker">Press Map For Marker</string>
|
||||
<string name="activity_view_marker">View Marker API</string>
|
||||
<string name="activity_add_remove_markers">Add/Remove marker</string>
|
||||
<string name="activity_add_remove_markers">Zoom function with SymbolLayer</string>
|
||||
<string name="activity_info_window">Standard InfoWindow</string>
|
||||
<string name="activity_infowindow_adapter">Custom InfoWindow</string>
|
||||
<string name="activity_dynamic_infowindow_adapter">Custom Dynamic InfoWindow</string>
|
||||
@@ -114,7 +114,7 @@
|
||||
<string name="description_simple_map">Shows a simple map</string>
|
||||
<string name="description_map_change">Logs map change events to Logcat</string>
|
||||
<string name="description_visibility_map">Changes visibility of map and view parent</string>
|
||||
<string name="description_add_remove_markers">Based on zoom level</string>
|
||||
<string name="description_add_remove_markers">Change Symbol icon when zoom levels changes</string>
|
||||
<string name="description_style_file">Use a local file as the style</string>
|
||||
<string name="description_map_in_dialog">Display a map inside a dialog fragment</string>
|
||||
<string name="description_marker_view_rectangle">Marker Views within a rectangle</string>
|
||||
|
||||
Reference in New Issue
Block a user