Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 57bc7fc

Browse files
author
Sam Fader
committed
add symbol listener example
1 parent 1ea902c commit 57bc7fc

File tree

10 files changed

+198
-0
lines changed

10 files changed

+198
-0
lines changed

MapboxAndroidDemo/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ android {
1010
compileSdkVersion androidVersions.compileSdkVersion
1111
buildToolsVersion androidVersions.buildToolsVersion
1212

13+
1314
playAccountConfigs {
1415
defaultAccountConfig {
1516
serviceAccountEmail = 'mapbox-android-demo-publish@android-gl-native.iam.gserviceaccount.com'
@@ -127,6 +128,7 @@ dependencies {
127128
implementation dependenciesList.mapboxPluginLocalization
128129
implementation dependenciesList.mapboxPluginTraffic
129130
implementation dependenciesList.mapboxPluginMarkerView
131+
implementation dependenciesList.mapboxPluginAnnotation
130132

131133
// Firebase
132134
globalImplementation dependenciesList.firebaseCrash
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<com.mapbox.mapboxsdk.maps.MapView
9+
android:id="@+id/mapView"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
mapbox:mapbox_cameraTargetLng="-43.334931"
13+
mapbox:mapbox_cameraZoom="0.346515"
14+
/>
15+
16+
<android.support.design.widget.FloatingActionButton
17+
android:id="@+id/fab_location_search"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:layout_gravity="end|bottom"
21+
android:layout_margin="16dp"
22+
android:tint="@android:color/white"
23+
app:backgroundTint="@color/colorPrimary"
24+
app:srcCompat="@android:drawable/ic_search_category_default" />
25+
26+
</android.support.design.widget.CoordinatorLayout>

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import com.mapbox.mapboxandroiddemo.examples.plugins.MarkerViewPluginActivity;
9999
import com.mapbox.mapboxandroiddemo.examples.plugins.PlaceSelectionPluginActivity;
100100
import com.mapbox.mapboxandroiddemo.examples.plugins.PlacesPluginActivity;
101+
import com.mapbox.mapboxandroiddemo.examples.plugins.SymbolListenerActivity;
101102
import com.mapbox.mapboxandroiddemo.examples.plugins.TrafficPluginActivity;
102103
import com.mapbox.mapboxandroiddemo.examples.query.BuildingOutlineActivity;
103104
import com.mapbox.mapboxandroiddemo.examples.query.ClickOnLayerActivity;
@@ -666,6 +667,13 @@ private void initializeModels() {
666667
null,
667668
R.string.activity_plugins_places_plugin_url, false, BuildConfig.MIN_SDK_VERSION));
668669

670+
exampleItemModels.add(new ExampleItemModel(
671+
R.id.nav_plugins,
672+
R.string.activity_plugins_symbol_listener_title, R.string.activity_plugins_symbol_listener_description,
673+
new Intent(MainActivity.this, SymbolListenerActivity.class),
674+
null,
675+
R.string.activity_plugins_symbol_listener_url, false, BuildConfig.MIN_SDK_VERSION));
676+
669677
exampleItemModels.add(new ExampleItemModel(
670678
R.id.nav_plugins,
671679
R.string.activity_plugins_localization_plugin_title,

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@
642642
android:name="android.support.PARENT_ACTIVITY"
643643
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
644644
</activity>
645+
<activity
646+
android:name=".examples.plugins.SymbolListenerActivity"
647+
android:label="Listen for Symbol interaction">
648+
<meta-data
649+
android:name="android.support.PARENT_ACTIVITY"
650+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
651+
</activity>
645652
<activity
646653
android:name=".examples.plugins.TrafficPluginActivity"
647654
android:label="@string/activity_plugins_traffic_plugin_title">
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.mapbox.mapboxandroiddemo.examples.plugins;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.v7.app.AppCompatActivity;
6+
7+
import com.mapbox.mapboxandroiddemo.R;
8+
import com.mapbox.mapboxsdk.Mapbox;
9+
import com.mapbox.mapboxsdk.geometry.LatLng;
10+
import com.mapbox.mapboxsdk.maps.MapView;
11+
import com.mapbox.mapboxsdk.maps.MapboxMap;
12+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
13+
import com.mapbox.mapboxsdk.maps.Style;
14+
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolLongClickListener;
15+
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager;
16+
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions;
17+
import com.mapbox.mapboxsdk.plugins.annotation.Symbol;
18+
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolClickListener;
19+
20+
/**
21+
* Change symbol icon by pressing on icon
22+
*/
23+
public class SymbolListenerActivity extends AppCompatActivity implements
24+
OnMapReadyCallback {
25+
26+
private MapView mapView;
27+
private MapboxMap mapboxMap;
28+
private static final String MAKI_ICON_CAFE = "cafe-15";
29+
private static final String MAKI_ICON_HARBOR = "harbor-15";
30+
private static final String MAKI_ICON_AIRPORT = "airport-15";
31+
private SymbolManager symbolManager;
32+
33+
@Override
34+
protected void onCreate(Bundle savedInstanceState) {
35+
super.onCreate(savedInstanceState);
36+
37+
// Mapbox access token is configured here. This needs to be called either in your application
38+
// object or in the same activity which contains the mapview.
39+
Mapbox.getInstance(this, getString(R.string.access_token));
40+
41+
// This contains the MapView in XML and needs to be called after the access token is configured.
42+
setContentView(R.layout.activity_annotation_plugin_symbol_activity);
43+
44+
mapView = findViewById(R.id.mapView);
45+
mapView.onCreate(savedInstanceState);
46+
mapView.getMapAsync(this);
47+
48+
}
49+
50+
@Override
51+
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
52+
53+
this.mapboxMap = mapboxMap;
54+
mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() {
55+
@Override
56+
public void onStyleLoaded(@NonNull Style style) {
57+
58+
// Set up a SymbolManager instance
59+
symbolManager = new SymbolManager(mapView, mapboxMap, style);
60+
61+
symbolManager.setIconAllowOverlap(true);
62+
symbolManager.setTextAllowOverlap(true);
63+
64+
// Add symbol at specified lat/lon
65+
symbolManager.create(new SymbolOptions()
66+
.withLatLng(new LatLng(60.169091, 24.939876))
67+
.withIconImage(MAKI_ICON_HARBOR)
68+
.withIconSize(2.0f));
69+
70+
// Add click listener and change the symbol to a cafe icon on click
71+
symbolManager.addClickListener(new OnSymbolClickListener() {
72+
@Override
73+
public void onAnnotationClick(Symbol symbol) {
74+
symbol.setIconImage(MAKI_ICON_CAFE);
75+
symbolManager.update(symbol);
76+
}});
77+
78+
// Add long click listener and change the symbol to an airport icon on long click
79+
symbolManager.addLongClickListener((new OnSymbolLongClickListener() {
80+
@Override
81+
public void onAnnotationLongClick(Symbol symbol) {
82+
symbol.setIconImage(MAKI_ICON_AIRPORT);
83+
symbolManager.update(symbol);
84+
}
85+
}));
86+
}
87+
});
88+
}
89+
90+
91+
@Override
92+
public void onResume() {
93+
super.onResume();
94+
mapView.onResume();
95+
}
96+
97+
@Override
98+
protected void onStart() {
99+
super.onStart();
100+
mapView.onStart();
101+
}
102+
103+
@Override
104+
protected void onStop() {
105+
super.onStop();
106+
mapView.onStop();
107+
}
108+
109+
@Override
110+
public void onPause() {
111+
super.onPause();
112+
mapView.onPause();
113+
}
114+
115+
@Override
116+
public void onLowMemory() {
117+
super.onLowMemory();
118+
mapView.onLowMemory();
119+
}
120+
121+
@Override
122+
protected void onDestroy() {
123+
super.onDestroy();
124+
mapView.onDestroy();
125+
}
126+
127+
@Override
128+
protected void onSaveInstanceState(Bundle outState) {
129+
super.onSaveInstanceState(outState);
130+
mapView.onSaveInstanceState(outState);
131+
}
132+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<com.mapbox.mapboxsdk.maps.MapView
9+
android:id="@+id/mapView"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
mapbox:mapbox_cameraTargetLng="24.939876"
13+
mapbox:mapbox_cameraTargetLat="60.169091"
14+
mapbox:mapbox_cameraZoom="12"
15+
/>
16+
17+
18+
</android.support.design.widget.CoordinatorLayout>

MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<string name="activity_plugins_building_plugin_description">Use the building plugin to easily display 3D building height</string>
8282
<string name="activity_plugins_geojson_plugin_description">Easily retrieve GeoJSON data from a url, asset, or path</string>
8383
<string name="activity_plugins_places_plugin_description">Add location search ("geocoding") functionality and UI to search for any place in the world</string>
84+
<string name="activity_plugins_symbol_listener_description">Listen for Symbol interaction using the Annotations plugin and built-in listeners</string>
8485
<string name="activity_plugins_localization_plugin_description">Use the plugin to automatically change map label text to the language set on the device.</string>
8586
<string name="activity_plugins_place_picker_plugin_description">Use the place picker function of the Places Plugin to choose a specific location in the world.</string>
8687
<string name="activity_plugins_markerview_plugin_description">Create a marker with an Android-system View.</string>

MapboxAndroidDemo/src/main/res/values/titles_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<string name="activity_plugins_localization_plugin_title">Change map text to device language</string>
8282
<string name="activity_plugins_geojson_plugin_title">Load GeoJSON data</string>
8383
<string name="activity_plugins_places_plugin_title">Location search</string>
84+
<string name="activity_plugins_symbol_listener_title">Symbol listener</string>
8485
<string name="activity_plugins_place_picker_plugin_title">Place picker</string>
8586
<string name="activity_plugins_markerview_plugin_title">MarkerView</string>
8687
<string name="activity_location_user_location_map_frag_title">Show a user\'s location on a map fragment</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<string name="activity_plugins_traffic_plugin_url" translatable="false">http://i.imgur.com/HRriOVR.png</string>
8080
<string name="activity_plugins_building_plugin_url" translatable="false">http://i.imgur.com/Vcu67UR.png</string>
8181
<string name="activity_plugins_places_plugin_url" translatable="false">https://i.imgur.com/oKHx3bv.png</string>
82+
<string name="activity_plugins_symbol_listener_url" translatable="false">https://i.imgur.com/r9fuJBJ.png</string>
8283
<string name="activity_plugins_geojson_plugin_url" translatable="false">http://i.imgur.com/kju0sKw.jpg</string>
8384
<string name="activity_plugins_localization_plugin_url" translatable="false">https://i.imgur.com/tsOM1am.png</string>
8485
<string name="activity_plugins_place_picker_plugin_url" translatable="false">https://i.imgur.com/fPZQg7z.png</string>

gradle/dependencies.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ext {
1919
mapboxPluginTraffic : '0.8.0',
2020
mapboxChinaPlugin : '2.1.0',
2121
mapboxPluginMarkerView : '0.2.0',
22+
mapboxPluginAnnotation : '0.5.0',
2223

2324
// Support
2425
supportLib : '28.0.0',
@@ -73,6 +74,7 @@ ext {
7374
mapboxPluginTraffic : "com.mapbox.mapboxsdk:mapbox-android-plugin-traffic-v7:${version.mapboxPluginTraffic}",
7475
mapboxChinaPlugin : "com.mapbox.mapboxsdk:mapbox-android-plugin-china:${version.mapboxChinaPlugin}",
7576
mapboxPluginMarkerView : "com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v7:${version.mapboxPluginMarkerView}",
77+
mapboxPluginAnnotation : "com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:${version.mapboxPluginAnnotation}",
7678

7779
// Support
7880
supportV4 : "com.android.support:support-v4:${version.supportLib}",

0 commit comments

Comments
 (0)