Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MarkerView plugin #652

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ dependencies {
implementation project(':plugin-offline')
implementation project(':plugin-localization')
implementation project(':plugin-annotation')
implementation project(':plugin-markerview')
}

apply from: "${rootDir}/gradle/checkstyle.gradle"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>
<activity
android:name=".activity.markerview.MarkerViewActivity"
android:description="@string/description_markerview"
android:label="@string/title_markerview">
<meta-data
android:name="@string/category"
android:value="@string/category_markerview" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>

<service
android:name="com.mapbox.mapboxsdk.plugins.offline.offline.OfflineDownloadService"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.mapbox.mapboxsdk.plugins.testapp.activity.markerview;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
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.plugins.markerview.MarkerView;
import com.mapbox.mapboxsdk.plugins.markerview.MarkerViewManager;
import com.mapbox.mapboxsdk.plugins.testapp.R;

import java.util.Random;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class MarkerViewActivity extends AppCompatActivity implements MapboxMap.OnMapLongClickListener,
MapboxMap.OnMapClickListener {

private final Random random = new Random();
private MarkerViewManager markerViewManager;
private MapView mapView;
private MarkerView marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annotation);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(mapboxMap -> {
mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(2));

markerViewManager = new MarkerViewManager(mapView, mapboxMap);
createCustomMarker();
createRandomMarkers();

mapboxMap.addOnMapLongClickListener(MarkerViewActivity.this);
mapboxMap.addOnMapClickListener(MarkerViewActivity.this);
});
}

private void createCustomMarker() {
// create a custom animation marker view
final View customView = createCustomAnimationView();
marker = new MarkerView(new LatLng(), customView);
markerViewManager.addMarker(marker);
}

private View createCustomAnimationView() {
View customView = LayoutInflater.from(this).inflate(R.layout.marker_view, null);
customView.setLayoutParams(new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
View icon = customView.findViewById(R.id.imageview);
View animationView = customView.findViewById(R.id.animation_layout);
icon.setOnClickListener(v -> {
ValueAnimator anim = ValueAnimator.ofInt(animationView.getMeasuredWidth(), 350);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.addUpdateListener(valueAnimator -> {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = animationView.getLayoutParams();
layoutParams.width = val;
animationView.setLayoutParams(layoutParams);
});
anim.setDuration(1250);
anim.start();
});
return customView;
}

private void createRandomMarkers() {
for (int i = 0; i < 20; i++) {
final ImageView imageView = new ImageView(MarkerViewActivity.this);
imageView.setImageResource(R.drawable.ic_car);
imageView.setLayoutParams(new FrameLayout.LayoutParams(56, 56));
MarkerView markerView = new MarkerView(createRandomLatLng(), imageView);
markerViewManager.addMarker(markerView);
}
}

private LatLng createRandomLatLng() {
return new LatLng((random.nextDouble() * -180.0) + 90.0,
(random.nextDouble() * -360.0) + 180.0);
}

@Override
public void onMapLongClick(@NonNull LatLng point) {
if (marker != null) {
markerViewManager.removeMarker(marker);
marker = null;
}
}

@Override
public void onMapClick(@NonNull LatLng point) {
if (marker != null) {
marker.setLatLng(point);
}
}

@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 onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

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

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
26 changes: 26 additions & 0 deletions app/src/main/res/layout/marker_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:background="@color/cardview_dark_background"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_height="32dp">

<ImageView
android:id="@+id/imageview"
app:srcCompat="@drawable/ic_car"
android:layout_width="32dp"
android:layout_alignParentLeft="true"
android:layout_height="32dp"
android:padding="4dp"/>

<FrameLayout
android:id="@+id/animation_layout"
android:layout_width="0dp"
android:layout_toRightOf="@id/imageview"
android:layout_height="32dp"/>

</android.support.v7.widget.CardView>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<string name="category_offline">Offline</string>
<string name="category_localization">Localization</string>
<string name="category_annotation">Annotations</string>
<string name="category_markerview">View synchronisation</string>

<!-- Titles -->
<string name="title_traffic">Traffic Plugin</string>
Expand All @@ -28,6 +29,7 @@
<string name="title_offline_ui_components">Offline Ui Components</string>
<string name="title_location_fragment">Location Fragment</string>
<string name="title_marker">Symbol plugin</string>
<string name="title_markerview">MarkerView plugin</string>

<!-- Descriptions -->
<string name="description_traffic">Add Traffic layers to any Mapbox basemap.</string>
Expand All @@ -45,6 +47,7 @@
<string name="description_offline_ui_components">Shows off the UI components included in the offline plugin.</string>
<string name="description_location_fragment">Show current location in the Fragment</string>
<string name="description_marker">Show symbols on a map</string>
<string name="description_markerview">Synchronise a view on a map</string>

<!-- Buttons -->
<string name="button_location_mode_none">None</string>
Expand Down
1 change: 1 addition & 0 deletions plugin-markerview/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
47 changes: 47 additions & 0 deletions plugin-markerview/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion androidVersions.compileSdkVersion
buildToolsVersion androidVersions.buildToolsVersion

defaultConfig {
minSdkVersion androidVersions.minSdkVersion
targetSdkVersion androidVersions.targetSdkVersion
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

configurations {
javadocDeps
}

lintOptions {
abortOnError false
}

testOptions {
unitTests.returnDefaultValues true
unitTests.all {
jacoco {
includeNoLocationClasses true
}
}
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

dependencies {
implementation dependenciesList.supportAppcompatV7
implementation dependenciesList.mapboxMapSdk
javadocDeps dependenciesList.mapboxMapSdk
testImplementation dependenciesList.junit
testImplementation dependenciesList.mockito
}

apply from: "${rootDir}/gradle/javadoc.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
apply from: "${rootDir}/gradle/checkstyle.gradle"
apply from: "${rootDir}/gradle/jacoco.gradle"
5 changes: 5 additions & 0 deletions plugin-markerview/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
VERSION_NAME=0.1.0-SNAPSHOT
POM_ARTIFACT_ID=mapbox-android-plugin-markerview
POM_NAME=Mapbox Android MarkerView Plugin
POM_DESCRIPTION=Mapbox Android MarkerView Plugin
POM_PACKAGING=aar
21 changes: 21 additions & 0 deletions plugin-markerview/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions plugin-markerview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapbox.mapboxsdk.plugins.markerview"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.mapbox.mapboxsdk.plugins.markerview;

import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.view.View;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.Projection;

/**
* MarkerView class wraps a latitude-longitude pair with a Android SDK View.
* <p>
* It can be used in conjuction with {@link MarkerViewManager} to synchronise the Android SDK View on top
* of map at the latitude-longitude pair.
* </p>
*/
public class MarkerView {

private final View view;
private LatLng latLng;
private Projection projection;
private OnPositionUpdateListener onPositionUpdateListener;

/**
* Create a MarkerView
*
* @param latLng latitude-longitude pair
* @param view an Android SDK View
*/
public MarkerView(@NonNull LatLng latLng, @NonNull View view) {
this.latLng = latLng;
this.view = view;
}

/**
* Update the location of the MarkerView on the map.
* <p>
* Provided as a latitude-longitude pair.
* </p>
*
* @param latLng latitude-longitude pair
*/
public void setLatLng(@NonNull LatLng latLng) {
this.latLng = latLng;
update();
}

/**
* Set a callback to be invoked when position placement is calculated.
* <p>
* Can be used to offset a MarkerView on screen.
* </p>
*
* @param onPositionUpdateListener callback to be invoked when position placement is calculated
*/
public void setOnPositionUpdateListener(OnPositionUpdateListener onPositionUpdateListener) {
this.onPositionUpdateListener = onPositionUpdateListener;
}

/**
* Callback definition that is invoked when position placement of a MarkerView is calculated.
*/
public interface OnPositionUpdateListener {
PointF onUpdate(PointF pointF);
}

void setProjection(Projection projection) {
this.projection = projection;
}

View getView() {
return view;
}

void update() {
PointF point = projection.toScreenLocation(latLng);
if (onPositionUpdateListener != null) {
point = onPositionUpdateListener.onUpdate(point);
}
view.setX(point.x);
view.setY(point.y);
}

}
Loading