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

Add symbol listener activity #1000

Merged
merged 13 commits into from
Mar 28, 2019
6 changes: 6 additions & 0 deletions MapboxAndroidDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ android {
compileSdkVersion androidVersions.compileSdkVersion
buildToolsVersion androidVersions.buildToolsVersion

compileOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this compileOptionssection anymore. I tried removing it and the example works just fine for me.

sourceCompatibility 1.8
targetCompatibility 1.8
}

playAccountConfigs {
defaultAccountConfig {
serviceAccountEmail = 'mapbox-android-demo-publish@android-gl-native.iam.gserviceaccount.com'
Expand Down Expand Up @@ -127,6 +132,7 @@ dependencies {
implementation dependenciesList.mapboxPluginLocalization
implementation dependenciesList.mapboxPluginTraffic
implementation dependenciesList.mapboxPluginMarkerView
implementation dependenciesList.mapboxPluginAnnotation

// Firebase
globalImplementation dependenciesList.firebaseCrash
Expand Down

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.plugins.SymbolListenerActivity"
android:label="Listen for Symbol interaction">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.plugins.TrafficPluginActivity"
android:label="@string/activity_plugins_traffic_plugin_title">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.mapbox.mapboxandroiddemo.examples.plugins;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
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.maps.Style;
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolDragListener;
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolLongClickListener;
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager;
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions;
import com.mapbox.mapboxsdk.plugins.annotation.Symbol;
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolClickListener;

import java.util.ArrayList;
import java.util.List;

/**
* Change symbol icon by pressing on icon
*/
public class SymbolListenerActivity extends AppCompatActivity implements
OnMapReadyCallback {

private MapView mapView;
private MapboxMap mapboxMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this. Field can be converted to a local variable (on line 62).

private static final String LOG = "SymbolListenerActivity";
private static final String MAKI_ICON_CAFE = "cafe-15";
private static final String MAKI_ICON_HARBOR = "harbor-15";
private static final String MAKI_ICON_AIRPORT = "airport-15";
private SymbolManager symbolManager;
private Symbol symbol;
private final List<ValueAnimator> animators = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this. You're not using it anywhere in the example.


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

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_annotation_plugin_symbol_activity);

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: delete this blank line 😬

}

@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {

this.mapboxMap = mapboxMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this. You don't use mapboxMap elsewhere.

mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {

// Set up a SymbolManager instance
symbolManager = new SymbolManager(mapView, mapboxMap, style);

symbolManager.setIconAllowOverlap(true);
symbolManager.setTextAllowOverlap(true);

// Add symbol at specified lat/lon
symbol = symbolManager.create(new SymbolOptions()
.withLatLng(new LatLng(60.169091, 24.939876))
.withIconImage(MAKI_ICON_HARBOR)
.withIconSize(2.0f)
.setDraggable(true));

// Add click listener and change the symbol to a cafe icon on click
symbolManager.addClickListener(new OnSymbolClickListener() {
@Override
public void onAnnotationClick(Symbol symbol) {
Toast.makeText(SymbolListenerActivity.this,
String.format("Symbol clicked"),
Toast.LENGTH_SHORT).show();
symbol.setIconImage(MAKI_ICON_CAFE);
symbolManager.update(symbol);
}
});

// Add long click listener and change the symbol to an airport icon on long click
symbolManager.addLongClickListener((new OnSymbolLongClickListener() {
@Override
public void onAnnotationLongClick(Symbol symbol) {
Toast.makeText(SymbolListenerActivity.this,
String.format("Symbol long clicked"),
Toast.LENGTH_SHORT).show();
symbol.setIconImage(MAKI_ICON_AIRPORT);
symbolManager.update(symbol);
}
}));

symbolManager.addDragListener(new OnSymbolDragListener() {
@Override
public void onAnnotationDragStarted(Symbol annotation) {
langsmith marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void onAnnotationDrag(Symbol symbol) {
}
langsmith marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void onAnnotationDragFinished(Symbol annotation) {
}
langsmith marked this conversation as resolved.
Show resolved Hide resolved
});
}
});
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLng="24.939876"
mapbox:mapbox_cameraTargetLat="60.169091"
mapbox:mapbox_cameraZoom="12"
/>

</android.support.design.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<string name="activity_plugins_building_plugin_description">Use the building plugin to easily display 3D building height</string>
<string name="activity_plugins_geojson_plugin_description">Easily retrieve GeoJSON data from a url, asset, or path</string>
<string name="activity_plugins_places_plugin_description">Add location search ("geocoding") functionality and UI to search for any place in the world</string>
<string name="activity_plugins_symbol_listener_description">Listen for Symbol interaction using the Annotations plugin and built-in listeners</string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<string name="activity_plugins_symbol_listener_description">Listen for Symbol interaction using the Annotations plugin and built-in listeners</string>
<string name="activity_plugins_symbol_listener_description">Listen for Symbol interaction using the Annotation plugin and its built-in listeners</string>

<string name="activity_plugins_localization_plugin_description">Use the plugin to automatically change map label text to the language set on the device.</string>
<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>
<string name="activity_plugins_markerview_plugin_description">Create a marker with an Android-system View.</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/titles_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<string name="activity_plugins_localization_plugin_title">Change map text to device language</string>
<string name="activity_plugins_geojson_plugin_title">Load GeoJSON data</string>
<string name="activity_plugins_places_plugin_title">Location search</string>
<string name="activity_plugins_symbol_listener_title">Symbol listener</string>
<string name="activity_plugins_place_picker_plugin_title">Place picker</string>
<string name="activity_plugins_markerview_plugin_title">MarkerView</string>
<string name="activity_location_user_location_map_frag_title">Show a user\'s location on a map fragment</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/urls_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<string name="activity_plugins_traffic_plugin_url" translatable="false">http://i.imgur.com/HRriOVR.png</string>
<string name="activity_plugins_building_plugin_url" translatable="false">http://i.imgur.com/Vcu67UR.png</string>
<string name="activity_plugins_places_plugin_url" translatable="false">https://i.imgur.com/oKHx3bv.png</string>
<string name="activity_plugins_symbol_listener_url" translatable="false">https://i.imgur.com/r9fuJBJ.png</string>
<string name="activity_plugins_geojson_plugin_url" translatable="false">http://i.imgur.com/kju0sKw.jpg</string>
<string name="activity_plugins_localization_plugin_url" translatable="false">https://i.imgur.com/tsOM1am.png</string>
<string name="activity_plugins_place_picker_plugin_url" translatable="false">https://i.imgur.com/fPZQg7z.png</string>
Expand Down
2 changes: 2 additions & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ext {
mapboxPluginTraffic : '0.8.0',
mapboxChinaPlugin : '2.1.0',
mapboxPluginMarkerView : '0.2.0',
mapboxPluginAnnotation : '0.5.0',

// Support
supportLib : '28.0.0',
Expand Down Expand Up @@ -73,6 +74,7 @@ ext {
mapboxPluginTraffic : "com.mapbox.mapboxsdk:mapbox-android-plugin-traffic-v7:${version.mapboxPluginTraffic}",
mapboxChinaPlugin : "com.mapbox.mapboxsdk:mapbox-android-plugin-china:${version.mapboxChinaPlugin}",
mapboxPluginMarkerView : "com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v7:${version.mapboxPluginMarkerView}",
mapboxPluginAnnotation : "com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:${version.mapboxPluginAnnotation}",

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