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

Commit

Permalink
Places plugin geometry –> point casting to replace coordinate extract…
Browse files Browse the repository at this point in the history
…ion (#626)

* added casting to replace extraction

* removed unneeded methods
  • Loading branch information
Langston Smith authored Feb 20, 2018
1 parent d5d0b55 commit 48a65fd
Showing 1 changed file with 25 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void onMapReady(MapboxMap mapboxMap) {

// Add the symbol layer icon to map for future use
Bitmap icon = BitmapFactory.decodeResource(
PlacesPluginActivity.this.getResources(), R.drawable.blue_marker_view);
PlacesPluginActivity.this.getResources(), R.drawable.blue_marker_view);
mapboxMap.addImage(symbolIconId, icon);

// Create an empty GeoJSON source using the empty feature collection
Expand All @@ -79,33 +79,33 @@ private void initSearchFab() {
@Override
public void onClick(View view) {
Intent intent = new PlaceAutocomplete.IntentBuilder()
.accessToken(Mapbox.getAccessToken())
.placeOptions(PlaceOptions.builder()
.backgroundColor(Color.parseColor("#EEEEEE"))
.limit(10)
.addInjectedFeature(home)
.addInjectedFeature(work)
.build(PlaceOptions.MODE_CARDS))
.build(PlacesPluginActivity.this);
.accessToken(Mapbox.getAccessToken())
.placeOptions(PlaceOptions.builder()
.backgroundColor(Color.parseColor("#EEEEEE"))
.limit(10)
.addInjectedFeature(home)
.addInjectedFeature(work)
.build(PlaceOptions.MODE_CARDS))
.build(PlacesPluginActivity.this);
startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
}
});
}

private void addUserLocations() {
home = CarmenFeature.builder().text("Mapbox SF Office")
.geometry(Point.fromLngLat(-122.399854, 37.7884400))
.placeName("85 2nd St, San Francisco, CA")
.id("mapbox-sf")
.properties(new JsonObject())
.build();
.geometry(Point.fromLngLat(-122.399854, 37.7884400))
.placeName("85 2nd St, San Francisco, CA")
.id("mapbox-sf")
.properties(new JsonObject())
.build();

work = CarmenFeature.builder().text("Mapbox DC Office")
.placeName("740 15th Street NW, Washington DC")
.geometry(Point.fromLngLat(-77.0338348, 38.899750))
.id("mapbox-dc")
.properties(new JsonObject())
.build();
.placeName("740 15th Street NW, Washington DC")
.geometry(Point.fromLngLat(-77.0338348, 38.899750))
.id("mapbox-dc")
.properties(new JsonObject())
.build();
}

private void setUpSource() {
Expand All @@ -129,7 +129,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// Create a new FeatureCollection and add a new Feature to it using selectedCarmenFeature above
FeatureCollection featureCollection = FeatureCollection.fromFeatures(
new Feature[] {Feature.fromJson(selectedCarmenFeature.toJson())});
new Feature[]{Feature.fromJson(selectedCarmenFeature.toJson())});

// Retrieve and update the source designated for showing a selected location's symbol layer icon
GeoJsonSource source = mapboxMap.getSourceAs(geojsonSourceLayerId);
Expand All @@ -139,33 +139,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// Move map camera to the selected location
CameraPosition newCameraPosition = new CameraPosition.Builder()
.target(new LatLng(getFeatureLat(selectedCarmenFeature), getFeatureLong(selectedCarmenFeature)))
.zoom(14)
.build();
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(newCameraPosition),4000);
.target(new LatLng(((Point) selectedCarmenFeature.geometry()).latitude(),
((Point) selectedCarmenFeature.geometry()).longitude()))
.zoom(14)
.build();
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(newCameraPosition), 4000);
}
}

// Extracts latitude from single GeoJSON Feature
private double getFeatureLat(CarmenFeature singleFeature) {
String[] coordinateValues = singleFeature.geometry()
.coordinates().toString().replace("Position [", "")
.replace(", altitude=NaN]", "").replace("longitude=", "")
.replace("]", "")
.split(", ");
return Double.valueOf(coordinateValues[1].replace("latitude=", ""));
}

// Extracts longitude from single GeoJSON Feature
private double getFeatureLong(CarmenFeature singleFeature) {
String[] coordinateValues = singleFeature.geometry()
.coordinates().toString().replace("Position [", "")
.replace(", altitude=NaN]", "").replace("longitude=", "")
.replace("[", "")
.split(", ");
return Double.valueOf(coordinateValues[0]);
}

// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
Expand Down

0 comments on commit 48a65fd

Please sign in to comment.