Skip to content

Commit

Permalink
Merge pull request #1 from AMOS-Project-TU-Berlin-Team-2/basicNavigation
Browse files Browse the repository at this point in the history
Basic navigation
  • Loading branch information
landgenoot authored Nov 29, 2018
2 parents 80408ad + e824009 commit b57195e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 41 deletions.
107 changes: 69 additions & 38 deletions app/src/main/java/amos/corridornavigation/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;

import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.models.BannerInstructions;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.api.directions.v5.models.RouteLeg;
import com.mapbox.api.directions.v5.models.StepManeuver;
import com.mapbox.api.geocoding.v5.MapboxGeocoding;
import com.mapbox.api.geocoding.v5.models.CarmenFeature;
import com.mapbox.api.geocoding.v5.models.GeocodingResponse;
Expand All @@ -19,7 +26,13 @@

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.ArrayList;
import java.util.List;

import retrofit2.Call;
Expand All @@ -29,8 +42,7 @@

// classes needed to add location layer

public class MainActivity extends MapContext
{
public class MainActivity extends MapContext {
public static String[] countries;

@Override
Expand Down Expand Up @@ -63,11 +75,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();
}
}
Expand All @@ -76,37 +86,37 @@ public void onSearchStart(CharSequence s) {
String addressPart = s.toString();
String[] dropdownSuggestions = getSuggestions(addressPart);
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.main_searchbar_input);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, dropdownSuggestions);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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 &amp; 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 &amp; Miquelon","Samoa","San Marino","Satellite","Saudi Arabia","Senegal","Serbia",
"Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","South Africa","South Korea","Spain",
"Sri Lanka","St Kitts &amp; Nevis","St Lucia","St Vincent","St. Lucia","Sudan","Suriname","Swaziland",
"Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga",
"Trinidad &amp; Tobago","Tunisia","Turkey","Turkmenistan","Turks &amp; 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 &amp; 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 &amp; Miquelon", "Samoa", "San Marino", "Satellite", "Saudi Arabia", "Senegal", "Serbia",
"Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "South Africa", "South Korea", "Spain",
"Sri Lanka", "St Kitts &amp; Nevis", "St Lucia", "St Vincent", "St. Lucia", "Sudan", "Suriname", "Swaziland",
"Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor L'Este", "Togo", "Tonga",
"Trinidad &amp; Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks &amp; Caicos", "Uganda", "Ukraine",
"United Arab Emirates", "United Kingdom", "Uruguay", "Uzbekistan", "Venezuela", "Vietnam", "Virgin Islands (US)", "Yemen", "Zambia", "Zimbabwe"};
}

public void onSearchButtonClicked(View view) {
Expand All @@ -121,15 +131,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)
Expand All @@ -140,14 +148,13 @@ public void onSearchButtonClicked(View view) {
public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {

List<CarmenFeature> 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
}
}

Expand All @@ -163,4 +170,28 @@ public void onFailure(Call<GeocodingResponse> call, Throwable throwable) {
}

}

public void onNavigateButtonClicked(View view) {

try {
boolean simulateRoute = true;
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.directionsRoute(super.locationMarker.currentRoute)
.shouldSimulateRoute(simulateRoute)
.build();


int max=options.directionsRoute().legs().get(0).steps().size();
for(int i = 0; i<max;i++) {
Log.d("instruction", options.directionsRoute().legs().get(0).steps().get(i).maneuver().instruction());
}

// 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();
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/amos/corridornavigation/MapContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/amos/corridornavigation/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
public class Router {

private Marker destinationMarkerPosition;
private LatLng destinationCoord;
protected LatLng destinationCoord;

private DirectionsRoute currentRoute;
protected DirectionsRoute currentRoute;
private NavigationMapRoute navigationMapRoute;

public Router()
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
android:padding="10dp"
android:text="@string/search"
android:textColor="#ffffff" />

<Button
android:id="@+id/Navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onNavigateButtonClicked"
android:text="@string/startnavi" />
</LinearLayout>

<android.support.design.widget.FloatingActionButton
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<string name="user_location_not_available">The user location is loading</string>
<string name="search">Search</string>
<string name="address">Address</string>
<string name="startnavi">StartNavi</string>
</resources>

0 comments on commit b57195e

Please sign in to comment.