Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

5282 locationpicker #5391

Merged
merged 2 commits into from
Jun 17, 2016
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 @@ -8,6 +8,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;
Expand Down Expand Up @@ -376,11 +377,7 @@ public void onClick(final View v) {
}

if (!clickHandled) {
// InfoWindow offset
int infoWindowOffsetX = (int) ((adaptedView.getWidth() * marker.getInfoWindowAnchorU()) - marker.getOffsetX());
int infoWindowOffsetY = (int) ((adaptedView.getHeight() * marker.getInfoWindowAnchorV()) - marker.getOffsetY());
marker.setTopOffsetPixels(infoWindowOffsetY);
marker.setRightOffsetPixels(infoWindowOffsetX);
ensureInfoWindowOffset(marker);
select(marker, v, adapter);
}
}
Expand All @@ -397,6 +394,44 @@ public void onClick(final View v) {
}
}

//TODO: This whole method is a stopgap for: https://github.com/mapbox/mapbox-gl-native/issues/5384
public void ensureInfoWindowOffset(MarkerView marker) {
View view = null;
if (markerViewMap.containsKey(marker)) {
view = markerViewMap.get(marker);
} else {
for (final MapboxMap.MarkerViewAdapter adapter : markerViewAdapters) {
if (adapter.getMarkerClass().equals(marker.getClass())) {
View convertView = (View) adapter.getViewReusePool().acquire();
view = adapter.getView(marker, convertView, mapView);
break;
}
}
}

if (view != null) {
//Ensure the marker's view is measured first
if (view.getMeasuredWidth() == 0) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
}

// update position on map
if (marker.getOffsetX() == -1) {
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
int x = (int) (marker.getAnchorU() * view.getMeasuredWidth());
int y = (int) (marker.getAnchorV() * view.getMeasuredHeight());
marker.setOffsetX(x);
marker.setOffsetY(y);
}

// InfoWindow offset
int infoWindowOffsetX = (int) ((view.getMeasuredWidth() * marker.getInfoWindowAnchorU()) - marker.getOffsetX());
int infoWindowOffsetY = (int) ((view.getMeasuredHeight() * marker.getInfoWindowAnchorV()) - marker.getOffsetY());
marker.setTopOffsetPixels(infoWindowOffsetY);
marker.setRightOffsetPixels(infoWindowOffsetX);
}
}

/**
* Default MarkerViewAdapter used for base class of MarkerView to adapt a MarkerView to an ImageView
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,11 @@ public void selectMarker(@NonNull Marker marker) {
}

if (!handledDefaultClick) {

if (marker instanceof MarkerView) {
mMarkerViewManager.ensureInfoWindowOffset((MarkerView) marker);
}

if (isInfoWindowValidForMarker(marker) || getInfoWindowAdapter() != null) {
mInfoWindows.add(marker.showInfoWindow(this, mMapView));
}
Expand Down
9 changes: 2 additions & 7 deletions platform/android/MapboxGLAndroidSDKTestApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,8 @@ dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta1'

// Directions SDK
compile('com.mapbox.mapboxsdk:mapbox-android-directions:1.0.0@aar') {
transitive = true
}

// Geocoder SDK
compile('com.mapbox.mapboxsdk:mapbox-android-geocoder:1.0.0@aar') {
// Mapbox Android Services
compile('com.mapbox.mapboxsdk:mapbox-android-services:1.1.0@aar') {
transitive = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@
android:value="@string/category_annotation" />
</activity>

<activity android:name=".activity.navigation.LocationPickerActivity"
android:description="@string/description_location_picker"
android:label="@string/activity_location_picker">
<meta-data
android:name="@string/category"
android:value="@string/category_navigation" />
</activity>

<!-- Configuration Settings -->
<meta-data
android:name="com.mapbox.TestEventsServer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import com.mapbox.directions.DirectionsCriteria;
import com.mapbox.directions.MapboxDirections;
import com.mapbox.directions.service.models.DirectionsResponse;
import com.mapbox.directions.service.models.DirectionsRoute;
import com.mapbox.directions.service.models.Waypoint;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
Expand All @@ -23,12 +17,18 @@
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.maps.MapView;

import java.util.List;

import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;
import com.mapbox.services.Constants;
import com.mapbox.services.commons.ServicesException;
import com.mapbox.services.commons.geojson.LineString;
import com.mapbox.services.commons.models.Position;
import com.mapbox.services.directions.v4.DirectionsCriteria;
import com.mapbox.services.directions.v4.MapboxDirections;
import com.mapbox.services.directions.v4.models.DirectionsResponse;
import com.mapbox.services.directions.v4.models.DirectionsRoute;
import com.mapbox.services.directions.v4.models.Waypoint;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DirectionsActivity extends AppCompatActivity {

Expand Down Expand Up @@ -94,49 +94,55 @@ private void loadRoute() {
}

private void getRoute(Waypoint origin, Waypoint destination) {
MapboxDirections md = new MapboxDirections.Builder()
.setAccessToken(getString(R.string.mapbox_access_token))
.setOrigin(origin)
.setDestination(destination)
.setProfile(DirectionsCriteria.PROFILE_WALKING)
.build();

md.enqueue(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Response<DirectionsResponse> response, Retrofit retrofit) {
// You can get generic HTTP info about the response
Log.d(LOG_TAG, "Response code: " + response.code());

// Print some info about the route
DirectionsRoute currentRoute = response.body().getRoutes().get(0);
Log.d(LOG_TAG, "Distance: " + currentRoute.getDistance());

// Draw the route on the map
drawRoute(currentRoute);
}

@Override
public void onFailure(Throwable t) {
Log.e(LOG_TAG, "Error: " + t.getMessage());
}
});
try {
MapboxDirections md = new MapboxDirections.Builder()
.setAccessToken(getString(R.string.mapbox_access_token))
.setOrigin(origin)
.setDestination(destination)
.setProfile(DirectionsCriteria.PROFILE_WALKING)
.build();

md.enqueueCall(new Callback<DirectionsResponse>() {

@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
Log.e(LOG_TAG, "Error: " + t.getMessage());
}

@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// You can get generic HTTP info about the response
Log.d(LOG_TAG, "Response code: " + response.code());

// Print some info about the route
DirectionsRoute currentRoute = response.body().getRoutes().get(0);
Log.d(LOG_TAG, "Distance: " + currentRoute.getDistance());

// Draw the route on the map
drawRoute(currentRoute);
}

});
} catch (ServicesException e) {
Log.e(LOG_TAG, "Error displaying route: " + e.toString());
e.printStackTrace();
}
}

private void drawRoute(DirectionsRoute route) {
// Convert List<Waypoint> into LatLng[]
List<Waypoint> waypoints = route.getGeometry().getWaypoints();
LatLng[] point = new LatLng[waypoints.size()];
for (int i = 0; i < waypoints.size(); i++) {
point[i] = new LatLng(
waypoints.get(i).getLatitude(),
waypoints.get(i).getLongitude());

PolylineOptions builder = new PolylineOptions();
builder.color(Color.parseColor("#3887be"));
builder.alpha(0.5f);
builder.width(5);
builder.width(5);
LineString lineString = route.asLineString(Constants.OSRM_PRECISION_V4);
for (Position coordinates : lineString.getCoordinates()) {
builder.add(new LatLng(coordinates.getLatitude(), coordinates.getLongitude()));
}

// Draw Points on MapView
mMapboxMap.addPolyline(new PolylineOptions()
.add(point)
.color(Color.parseColor("#3887be"))
.width(5));
mMapboxMap.addPolyline(builder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapbox.geocoder.GeocoderCriteria;
import com.mapbox.geocoder.MapboxGeocoder;
import com.mapbox.geocoder.service.models.GeocoderFeature;
import com.mapbox.geocoder.service.models.GeocoderResponse;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand All @@ -27,12 +22,19 @@
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.services.commons.ServicesException;
import com.mapbox.services.commons.models.Position;
import com.mapbox.services.geocoding.v5.GeocodingCriteria;
import com.mapbox.services.geocoding.v5.MapboxGeocoding;
import com.mapbox.services.geocoding.v5.models.GeocodingFeature;
import com.mapbox.services.geocoding.v5.models.GeocodingResponse;

import java.util.List;

import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class GeocoderActivity extends AppCompatActivity {

Expand Down Expand Up @@ -123,40 +125,38 @@ protected void onSaveInstanceState(Bundle outState) {
*/

private void geocode(final LatLng point) {
new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... params) {
MapboxGeocoder client = new MapboxGeocoder.Builder()
.setAccessToken(getString(R.string.mapbox_access_token))
.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());
}
}

);
return null;
}
}.execute();
try {
MapboxGeocoding client = new MapboxGeocoding.Builder()
.setAccessToken(getString(R.string.mapbox_access_token))
.setCoordinates(Position.fromCoordinates(point.getLongitude(), point.getLatitude()))
.setType(GeocodingCriteria.TYPE_POI)
.build();

client.enqueueCall(new Callback<GeocodingResponse>() {
@Override
public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {

List<GeocodingFeature> results = response.body().getFeatures();
if (results.size() > 0) {
String placeName = results.get(0).getPlaceName();
setSuccess(placeName);
} else {
setMessage("No results.");
}

}

@Override
public void onFailure(Call<GeocodingResponse> call, Throwable t) {
setError(t.getMessage());
}
});
} catch (ServicesException e) {
Log.e(LOG_TAG, "Error geocoding: " + e.toString());
e.printStackTrace();
setError(e.getMessage());
}
}

/*
Expand Down
Loading