From bc2eba7864d56004a043f5fa110ad8c7393dd640 Mon Sep 17 00:00:00 2001 From: RicoGetup Date: Sun, 25 Nov 2018 12:15:35 +0100 Subject: [PATCH 1/4] NavigationLauncherOptions Navigation --- .../amos/corridornavigation/MainActivity.java | 101 +++++++++++------- .../amos/corridornavigation/MapContext.java | 2 +- .../java/amos/corridornavigation/Router.java | 4 +- app/src/main/res/layout/activity_main.xml | 7 ++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 74 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/amos/corridornavigation/MainActivity.java b/app/src/main/java/amos/corridornavigation/MainActivity.java index 93e3719..e404923 100644 --- a/app/src/main/java/amos/corridornavigation/MainActivity.java +++ b/app/src/main/java/amos/corridornavigation/MainActivity.java @@ -10,6 +10,8 @@ import android.widget.EditText; import android.widget.Toast; +import com.mapbox.api.directions.v5.DirectionsCriteria; +import com.mapbox.api.directions.v5.models.DirectionsRoute; import com.mapbox.api.geocoding.v5.MapboxGeocoding; import com.mapbox.api.geocoding.v5.models.CarmenFeature; import com.mapbox.api.geocoding.v5.models.GeocodingResponse; @@ -19,6 +21,11 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.services.android.navigation.ui.v5.NavigationLauncher; +import com.mapbox.services.android.navigation.ui.v5.NavigationLauncherOptions; +import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation; +import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigationOptions; +import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute; import java.util.List; @@ -29,8 +36,7 @@ // classes needed to add location layer -public class MainActivity extends MapContext -{ +public class MainActivity extends MapContext { public static String[] countries; @Override @@ -63,11 +69,9 @@ public void afterTextChanged(Editable s) { } public void actionButtonPressed(View view) { - if(super.originLocation != null) { + if (super.originLocation != null) { this.setCameraPosition(super.originLocation); - } - else - { + } else { Toast.makeText(this, R.string.user_location_not_available, Toast.LENGTH_LONG).show(); } } @@ -76,37 +80,37 @@ public void onSearchStart(CharSequence s) { String addressPart = s.toString(); String[] dropdownSuggestions = getSuggestions(addressPart); AutoCompleteTextView autoCompleteTextView = findViewById(R.id.main_searchbar_input); - ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, dropdownSuggestions); + ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dropdownSuggestions); autoCompleteTextView.setAdapter(adapter); } // TOTO-make getSuggestion return list of suggested addresses, now it just returns // hardcoded list of countries for demoing and testing. private String[] getSuggestions(String addressPart) { - return new String[] {"Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua; Barbuda", - "Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh", - "Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina", - "Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia", - "Cameroon","Cape Verde","Cayman Islands","Chad","Chile","China","Colombia","Congo","Cook Islands", - "Costa Rica","Cote D Ivoire","Croatia","Cruise Ship","Cuba","Cyprus","Czech Republic","Denmark", - "Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea", - "Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia", - "French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland", - "Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras", - "Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel", - "Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kuwait","Kyrgyz Republic","Laos", - "Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau", - "Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Mauritania","Mauritius", - "Mexico","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Namibia", - "Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger", - "Nigeria","Norway","Oman","Pakistan","Palestine","Panama","Papua New Guinea","Paraguay","Peru", - "Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda", - "Saint Pierre & Miquelon","Samoa","San Marino","Satellite","Saudi Arabia","Senegal","Serbia", - "Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","South Africa","South Korea","Spain", - "Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","St. Lucia","Sudan","Suriname","Swaziland", - "Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga", - "Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Uganda","Ukraine", - "United Arab Emirates","United Kingdom","Uruguay","Uzbekistan","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"}; + return new String[]{"Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua; Barbuda", + "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", + "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia & Herzegovina", + "Botswana", "Brazil", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", + "Cameroon", "Cape Verde", "Cayman Islands", "Chad", "Chile", "China", "Colombia", "Congo", "Cook Islands", + "Costa Rica", "Cote D Ivoire", "Croatia", "Cruise Ship", "Cuba", "Cyprus", "Czech Republic", "Denmark", + "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", + "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Polynesia", + "French West Indies", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", + "Grenada", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea Bissau", "Guyana", "Haiti", "Honduras", + "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Isle of Man", "Israel", + "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kuwait", "Kyrgyz Republic", "Laos", + "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", + "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius", + "Mexico", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Namibia", + "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", + "Nigeria", "Norway", "Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru", + "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", + "Saint Pierre & Miquelon", "Samoa", "San Marino", "Satellite", "Saudi Arabia", "Senegal", "Serbia", + "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "South Africa", "South Korea", "Spain", + "Sri Lanka", "St Kitts & Nevis", "St Lucia", "St Vincent", "St. Lucia", "Sudan", "Suriname", "Swaziland", + "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor L'Este", "Togo", "Tonga", + "Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks & Caicos", "Uganda", "Ukraine", + "United Arab Emirates", "United Kingdom", "Uruguay", "Uzbekistan", "Venezuela", "Vietnam", "Virgin Islands (US)", "Yemen", "Zambia", "Zimbabwe"}; } public void onSearchButtonClicked(View view) { @@ -121,15 +125,13 @@ public void onSearchButtonClicked(View view) { String address = editText.getText().toString(); MapboxGeocoding client; try { - if(super.originLocation != null) { + if (super.originLocation != null) { client = MapboxGeocoding.builder() .accessToken(getString(R.string.access_token)) .proximity(Point.fromLngLat(super.originLocation.getLongitude(), super.originLocation.getLatitude())) .query(address) .build(); - } - else - { + } else { client = MapboxGeocoding.builder() .accessToken(getString(R.string.access_token)) .query(address) @@ -140,14 +142,13 @@ public void onSearchButtonClicked(View view) { public void onResponse(Call call, Response response) { List results = response.body().features(); - if(results.size() > 1) - { + if (results.size() > 1) { LatLng latLng = new LatLng(); latLng.setLatitude(results.get(0).center().latitude()); latLng.setLongitude(results.get(0).center().longitude()); onMapClick(latLng); - mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,13.0)); // mapboxMap came from MapContext + mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13.0)); // mapboxMap came from MapContext } } @@ -163,4 +164,28 @@ public void onFailure(Call call, Throwable throwable) { } } + + public void onNavigateButtonClicked(View view) { + + try { + boolean simulateRoute = false; + NavigationLauncherOptions options = NavigationLauncherOptions.builder() + .directionsRoute(super.locationMarker.currentRoute) + .shouldSimulateRoute(simulateRoute) + .build(); + + NavigationRoute option = NavigationRoute.builder(this) + .accessToken(Mapbox.getAccessToken()) + .origin(Point.fromLngLat(super.originLocation.getLongitude(),super.originLocation.getLatitude())) + .destination(Point.fromLngLat(super.locationMarker.destinationCoord.getLongitude(),super.locationMarker.destinationCoord.getLatitude())) + .voiceUnits(DirectionsCriteria.METRIC) + .build(); + // Call this method with Context from within an Activity + NavigationLauncher.startNavigation(this, options); + + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(this, "You may not have selected a route yet.", Toast.LENGTH_LONG).show(); + } + } } diff --git a/app/src/main/java/amos/corridornavigation/MapContext.java b/app/src/main/java/amos/corridornavigation/MapContext.java index bbcfc22..0d3d065 100644 --- a/app/src/main/java/amos/corridornavigation/MapContext.java +++ b/app/src/main/java/amos/corridornavigation/MapContext.java @@ -35,7 +35,7 @@ public class MapContext extends AppCompatActivity implements LocationEngineListe protected Location originLocation; - private Router locationMarker; + protected Router locationMarker; protected void initMapView(Bundle savedInstanceState) { diff --git a/app/src/main/java/amos/corridornavigation/Router.java b/app/src/main/java/amos/corridornavigation/Router.java index 0840420..a2b0730 100644 --- a/app/src/main/java/amos/corridornavigation/Router.java +++ b/app/src/main/java/amos/corridornavigation/Router.java @@ -20,9 +20,9 @@ public class Router { private Marker destinationMarkerPosition; - private LatLng destinationCoord; + protected LatLng destinationCoord; - private DirectionsRoute currentRoute; + protected DirectionsRoute currentRoute; private NavigationMapRoute navigationMapRoute; public Router() diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 66c391d..799be33 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -55,6 +55,13 @@ android:padding="10dp" android:text="@string/search" android:textColor="#ffffff" /> + +