summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorTobrun <tobrun@mapbox.com>2016-03-03 08:48:49 +0100
committerTobrun <tobrun@mapbox.com>2016-03-03 08:48:49 +0100
commite2da260a8ee0dd0b213ec0e30db2c6e3188c7c9b (patch)
tree35d5d8e775e0231aec80e171a31fe9b7029f2c33 /platform
parentd994f2de0fc9331d712b8dddded9e3792efa154f (diff)
downloadqtlocation-mapboxgl-e2da260a8ee0dd0b213ec0e30db2c6e3188c7c9b.tar.gz
[android] #4149 - added a drop pin example to the GeocoderActivity
Diffstat (limited to 'platform')
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/GeocoderActivity.java82
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable/ic_droppin_24dp.xml4
2 files changed, 60 insertions, 26 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/GeocoderActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/GeocoderActivity.java
index d826ae9b3a..5cc9b0da68 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/GeocoderActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/GeocoderActivity.java
@@ -1,12 +1,18 @@
package com.mapbox.mapboxsdk.testapp;
+import android.graphics.PointF;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
+import android.view.Gravity;
import android.view.MenuItem;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
import android.widget.TextView;
import com.mapbox.geocoder.GeocoderCriteria;
@@ -19,6 +25,7 @@ import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
+import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.maps.MapView;
@@ -55,11 +62,21 @@ public class GeocoderActivity extends AppCompatActivity {
mapView = (MapView) findViewById(R.id.mapView);
mapView.setAccessToken(ApiAccess.getToken(this));
- mapView.setStyle(Style.EMERALD);
+ mapView.setStyle(Style.MAPBOX_STREETS);
mapView.onCreate(savedInstanceState);
+
+ final ImageView dropPinView = new ImageView(this);
+ dropPinView.setImageResource(R.drawable.ic_droppin_24dp);
+ FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
+ dropPinView.setLayoutParams(params);
+ mapView.addView(dropPinView);
+
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
+ final Projection projection = mapboxMap.getProjection();
+ final int width = mapView.getMeasuredWidth();
+ final int height = mapView.getMeasuredHeight();
// Camera position
mapboxMap.setCameraPosition(
@@ -75,12 +92,15 @@ public class GeocoderActivity extends AppCompatActivity {
mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
+ PointF centerPoint = new PointF(width / 2, (height + dropPinView.getHeight()) / 2);
+ LatLng centerLatLng = new LatLng(projection.fromScreenLocation(centerPoint));
+
setMessage("Geocoding...");
+
mapboxMap.removeAnnotations();
- mapboxMap.addMarker(new MarkerOptions()
- .position(point)
- .title("Your finger is here"));
- geocode(point);
+ mapboxMap.addMarker(new MarkerOptions().position(centerLatLng));
+
+ geocode(centerLatLng);
}
});
}
@@ -127,31 +147,41 @@ public class GeocoderActivity extends AppCompatActivity {
* Forward geocoding
*/
- private void geocode(LatLng point) {
- MapboxGeocoder client = new MapboxGeocoder.Builder()
- .setAccessToken(ApiAccess.getToken(this))
- .setCoordinates(point.getLongitude(), point.getLatitude())
- .setType(GeocoderCriteria.TYPE_POI)
- .build();
+ private void geocode(final LatLng point) {
+ new AsyncTask<Void, Void, Void>() {
- client.enqueue(new Callback<GeocoderResponse>() {
@Override
- public void onResponse(Response<GeocoderResponse> response, Retrofit retrofit) {
- List<GeocoderFeature> results = response.body().getFeatures();
- if (results.size() > 0) {
- String placeName = results.get(0).getPlaceName();
- setSuccess(placeName);
- } else {
- setMessage("No results.");
- }
- }
+ protected Void doInBackground(Void... params) {
+ MapboxGeocoder client = new MapboxGeocoder.Builder()
+ .setAccessToken(ApiAccess.getToken(GeocoderActivity.this))
+ .setCoordinates(point.getLongitude(), point.getLatitude())
+ .setType(GeocoderCriteria.TYPE_POI)
+ .build();
+
+ client.enqueue(new Callback<GeocoderResponse>()
+
+ {
+ @Override
+ public void onResponse(Response<GeocoderResponse> response, Retrofit retrofit) {
+ List<GeocoderFeature> results = response.body().getFeatures();
+ if (results.size() > 0) {
+ String placeName = results.get(0).getPlaceName();
+ setSuccess(placeName);
+ } else {
+ setMessage("No results.");
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable t) {
+ setError(t.getMessage());
+ }
+ }
- @Override
- public void onFailure(Throwable t) {
- setError(t.getMessage());
+ );
+ return null;
}
- });
-
+ }.execute();
}
/*
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable/ic_droppin_24dp.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable/ic_droppin_24dp.xml
new file mode 100644
index 0000000000..a25e7884cd
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable/ic_droppin_24dp.xml
@@ -0,0 +1,4 @@
+<vector android:height="32dp" android:viewportHeight="24.0"
+ android:viewportWidth="24.0" android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
+ <path android:fillColor="#FF000000" android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zm0,9.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z"/>
+</vector>