Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Switch
import android.widget.Toast

import com.mapbox.mapboxsdk.Mapbox
Expand All @@ -20,12 +21,20 @@ class PickerLauncherActivity : AppCompatActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_picker_launcher)
reverseGeocodingSwitch.text = getString(R.string.reverse_geocoding_disabled)
reverseGeocodingSwitch.setOnCheckedChangeListener { compoundButton, checked ->
reverseGeocodingSwitch.text = if (checked)
getString(R.string.reverse_geocoding_enabled)
else getString(R.string.reverse_geocoding_disabled)
}

fabLocationPicker.setOnClickListener { _ ->
Mapbox.getAccessToken()?.let {
startActivityForResult(
PlacePicker.IntentBuilder()
.accessToken(it)
.placeOptions(PlacePickerOptions.builder()
.includeReverseGeocode(reverseGeocodingSwitch.isChecked)
.statingCameraPosition(CameraPosition.Builder()
.target(LatLng(40.7544, -73.9862))
.zoom(16.0)
Expand All @@ -39,8 +48,13 @@ class PickerLauncherActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val carmenFeature = PlacePicker.getPlace(data)
Toast.makeText(this, carmenFeature?.placeName(), Toast.LENGTH_LONG).show()
if (reverseGeocodingSwitch.isChecked) {
val carmenFeature = PlacePicker.getPlace(data)
Toast.makeText(this, carmenFeature?.placeName(), Toast.LENGTH_LONG).show()
} else {
val cameraPosition = PlacePicker.getLastCameraPosition(data)
Toast.makeText(this, cameraPosition.target.toString(), Toast.LENGTH_LONG).show()
}
}
}

Expand Down
77 changes: 46 additions & 31 deletions app/src/main/res/layout/activity_picker_launcher.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Switch
android:id="@+id/reverseGeocodingSwitch"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"/>

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/detailed_description_place_picker"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"/>
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/detailed_description_place_picker"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="@+id/reverseGeocodingSwitch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fabLocationPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:tint="@android:color/white"
app:backgroundTint="@color/colorPrimary"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right|end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/mapbox_ic_place"/>
android:id="@+id/fabLocationPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:tint="@android:color/white"
app:backgroundTint="@color/colorPrimary"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right|end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/mapbox_ic_place"/>

</android.support.constraint.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

<!-- Place picker -->
<string name="detailed_description_place_picker">Example shows how to launch the Place Picker using the Floating action button and receiving a result in onActivityResult.</string>
<string name="reverse_geocoding_enabled">Reverse geocoding enabled</string>
<string name="reverse_geocoding_disabled">Reverse geocoding disabled</string>

<!-- Misc -->
<string name="min_zoom_textview">Min zoom: %1$d</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public abstract class PlacePickerOptions implements BasePlaceOptions, Parcelable
@Nullable
public abstract CameraPosition statingCameraPosition();

public abstract boolean includeReverseGeocode();

public static Builder builder() {
return new AutoValue_PlacePickerOptions.Builder();
return new AutoValue_PlacePickerOptions.Builder()
.includeReverseGeocode(true);
}

@AutoValue.Builder
Expand All @@ -55,6 +58,16 @@ public Builder geocodingTypes(@NonNull @GeocodingTypeCriteria String... geocodin

public abstract Builder statingCameraPosition(@NonNull CameraPosition cameraPosition);

/**
*
* @param includeReverseGeocode whether or not to make a reverse geocoding call to
* retrieve and display information associated with
* the picked location's coordinates. Defaults to true.
*
* @return this builder instance for chaining options together
*/
public abstract Builder includeReverseGeocode(boolean includeReverseGeocode);

public abstract PlacePickerOptions build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class PlacePickerActivity extends AppCompatActivity implements OnMapReady
private MapboxMap mapboxMap;
private String accessToken;
private MapView mapView;
private boolean includeReverseGeocode;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -74,6 +75,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) {
accessToken = getIntent().getStringExtra(PlaceConstants.ACCESS_TOKEN);
options = getIntent().getParcelableExtra(PlaceConstants.PLACE_OPTIONS);
includeReverseGeocode = options.includeReverseGeocode();
}

// Initialize the view model.
Expand All @@ -82,7 +84,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

bindViews();
addBackButtonListener();
addChosenLocationButton();
addPlaceSelectedButton();
customizeViews();

mapView.onCreate(savedInstanceState);
Expand Down Expand Up @@ -129,8 +131,10 @@ public void onStyleLoaded(@NonNull Style style) {
}
}

// Initialize with the markers current location information.
makeReverseGeocodingSearch();
if (includeReverseGeocode) {
// Initialize with the markers current location information.
makeReverseGeocodingSearch();
}

PlacePickerActivity.this.mapboxMap.addOnCameraMoveStartedListener(PlacePickerActivity.this);
PlacePickerActivity.this.mapboxMap.addOnCameraIdleListener(PlacePickerActivity.this);
Expand All @@ -144,8 +148,10 @@ public void onCameraMoveStarted(int reason) {
if (markerImage.getTranslationY() == 0) {
markerImage.animate().translationY(-75)
.setInterpolator(new OvershootInterpolator()).setDuration(250).start();
if (bottomSheet.isShowing()) {
bottomSheet.dismissPlaceDetails();
if (includeReverseGeocode) {
if (bottomSheet.isShowing()) {
bottomSheet.dismissPlaceDetails();
}
}
}
}
Expand All @@ -155,8 +161,11 @@ public void onCameraIdle() {
Timber.v("Map camera is now idling.");
markerImage.animate().translationY(0)
.setInterpolator(new OvershootInterpolator()).setDuration(250).start();
bottomSheet.setPlaceDetails(null);
makeReverseGeocodingSearch();
if (includeReverseGeocode) {
bottomSheet.setPlaceDetails(null);
// Initialize with the markers current location information.
makeReverseGeocodingSearch();
}
}

@Override
Expand All @@ -182,12 +191,12 @@ private void makeReverseGeocodingSearch() {
}
}

private void addChosenLocationButton() {
private void addPlaceSelectedButton() {
FloatingActionButton placeSelectedButton = findViewById(R.id.place_chosen_button);
placeSelectedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (carmenFeature == null) {
if (carmenFeature == null && includeReverseGeocode) {
Snackbar.make(bottomSheet,
getString(R.string.mapbox_plugins_place_picker_not_valid_selection),
LENGTH_LONG).show();
Expand All @@ -199,9 +208,11 @@ public void onClick(View view) {
}

void placeSelected() {
String json = carmenFeature.toJson();
Intent returningIntent = new Intent();
returningIntent.putExtra(PlaceConstants.RETURNING_CARMEN_FEATURE, json);
if (includeReverseGeocode) {
String json = carmenFeature.toJson();
returningIntent.putExtra(PlaceConstants.RETURNING_CARMEN_FEATURE, json);
}
returningIntent.putExtra(PlaceConstants.MAP_CAMERA_POSITION, mapboxMap.getCameraPosition());
setResult(AppCompatActivity.RESULT_OK, returningIntent);
finish();
Expand Down