Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peter/reverse instructions #5

Merged
merged 8 commits into from
Jun 25, 2015
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
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<activity
android:name=".view.SearchResultsListActivity"
android:label="@string/search_results" />
<activity
android:name=".view.InstructionListActivity"
android:label="@string/instruction_list" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface MainPresenter {
public fun onSearchResultSelected(position: Int)
public fun onExpandSearchView()
public fun onCollapseSearchView()
public fun onShowDirectionList()
public fun onQuerySubmit()
public fun onViewAllSearchResults()
public fun onBackPressed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ public class MainPresenterImpl() : MainPresenter {
viewController?.shutDown()
}
}

override fun onShowDirectionList() {
viewController?.showDirectionList()
}
}
21 changes: 21 additions & 0 deletions app/src/main/kotlin/com/mapzen/erasermap/util/DisplayHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mapzen.erasermap.util

import android.content.Context
import com.mapzen.erasermap.R

public object DisplayHelper {

/**
* Fetch the resource drawable ID of the turn icon for the given instruction and style.

* @param context current context in which to display the icon.
* *
* @param turnInstruction the integer value representing this turn instruction.
* *
* @return the resource ID of the turn icon to display.
*/
public fun getRouteDrawable(context: Context, turnInstruction: Int?): Int {
var drawableId = context.getResources().getIdentifier("ic_route_" + turnInstruction, "drawable", context.getPackageName())
return drawableId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.mapzen.erasermap.view

import android.content.Context
import android.opengl.Visibility
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.ImageView
import com.mapzen.erasermap.R
import com.mapzen.erasermap.util.DisplayHelper
import com.mapzen.pelias.SimpleFeature
import com.mapzen.valhalla.Instruction
import com.mapzen.helpers.DistanceFormatter
import java.util.ArrayList

public class InstructionListActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_instructions)
getSupportActionBar()?.hide()
val listView = findViewById(R.id.instruction_list_view) as ListView
findViewById(R.id.route_reverse).setVisibility(View.GONE)
val bundle = getIntent()?.getExtras()
val instruction_strings: ArrayList<String>? = bundle?.getStringArrayList("instruction_strings")
val instruction_types: ArrayList<Int>? = bundle?.getIntegerArrayList("instruction_types")
val instruction_distances: ArrayList<Int>? = bundle?.getIntegerArrayList("instruction_distances")
val reverse : Boolean = bundle?.getBoolean("reverse") as Boolean
setHeaderOrigins(bundle, reverse)
if (instruction_strings != null) {
listView.setAdapter(DirectionListAdapter(this, instruction_strings, instruction_types, instruction_distances, reverse))
listView.setOnItemClickListener { parent, view, position, id ->
setResult(position)
finish()
}
}
}

private fun setHeaderOrigins(bundle: Bundle?, reverse: Boolean) {
if (reverse) {
(findViewById(R.id.starting_point) as TextView).setText(bundle?.getString("destination"))
(findViewById(R.id.destination) as TextView).setText(R.string.current_location)
findViewById(R.id.starting_location_icon).setVisibility(View.GONE)
findViewById(R.id.destination_location_icon).setVisibility(View.VISIBLE)
} else {
(findViewById(R.id.starting_point) as TextView).setText(R.string.current_location)
(findViewById(R.id.destination) as TextView).setText(bundle?.getString("destination"))
findViewById(R.id.starting_location_icon).setVisibility(View.VISIBLE)
findViewById(R.id.destination_location_icon).setVisibility(View.GONE)
}
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
setResult(-1)
finish()
return true
}

private class DirectionListAdapter(context: Context, strings: ArrayList<String>?,
types: ArrayList<Int>?, distances: ArrayList<Int>?,
reverse : Boolean) : BaseAdapter() {
private final var CURRENT_LOCATION_OFFSET = 1
private var instruction_strings: ArrayList<String>? = strings
private var instruction_types: ArrayList<Int>? = types
private var instruction_distances: ArrayList<Int>? = distances
private var context: Context = context
private var reverse : Boolean = reverse

override fun getCount(): Int {
var size = if (instruction_strings != null) (instruction_strings!!.size()
+ CURRENT_LOCATION_OFFSET) else 0
return size
}

override fun getItemId(position: Int): Long {
return 0
}

override fun getItem(position: Int): Any? {
return 0
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
var view: View = View.inflate(context, R.layout.direction_list_item, null)
if(reverse) {
setReversedDirectionListItem(position, view)
} else {
setDirectionListItem(position, view)
}
return view
}

private fun setReversedDirectionListItem(position : Int, view : View) {
if(position == instruction_strings?.size()) {
setListItemToCurrentLocation(view)
} else {
var distanceVal : Int? = instruction_distances?.get(position)
var formattedDistance : String = DistanceFormatter.format(
(distanceVal?.toInt() as Int))
var iconId : Int = DisplayHelper.getRouteDrawable(context,
instruction_types?.get(position))

(view.findViewById(R.id.simple_instruction) as TextView).setText(
instruction_strings?.get(position).toString())
(view.findViewById(R.id.distance) as TextView).setText(formattedDistance)
(view.findViewById(R.id.icon) as ImageView).setImageResource(iconId)
}
}

private fun setDirectionListItem(position : Int, view : View) {
if (position == 0 ) {
setListItemToCurrentLocation(view)
} else {
var distanceVal : Int? = instruction_distances?.get(position - CURRENT_LOCATION_OFFSET)
var formattedDistance : String = DistanceFormatter.format((distanceVal?.toInt() as Int))
var iconId : Int = DisplayHelper.getRouteDrawable(context,
instruction_types?.get(position - CURRENT_LOCATION_OFFSET))

(view.findViewById(R.id.simple_instruction) as TextView).setText(
instruction_strings?.get(position - CURRENT_LOCATION_OFFSET).toString())
(view.findViewById(R.id.distance) as TextView).setText(formattedDistance)
(view.findViewById(R.id.icon) as ImageView).setImageResource(iconId)
}
}

private fun setListItemToCurrentLocation(view : View) {
(view.findViewById(R.id.simple_instruction) as TextView).setText(R.string.current_location)
(view.findViewById(R.id.icon) as ImageView).setImageResource(R.drawable.ic_locate)
}
}

}
103 changes: 74 additions & 29 deletions app/src/main/kotlin/com/mapzen/erasermap/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback

public val requestCodeSearchResults: Int = 0x01

private var route: Route? = null;
var locationClient: LostApiClient? = null
@Inject set
var tileCache: Cache? = null
Expand All @@ -89,6 +90,8 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback
var destination: Feature? = null
var path: PathLayer? = null
var markers: ItemizedLayer<MarkerItem>? = null
var type : Router.Type = Router.Type.DRIVING
var reverse : Boolean = false;

override fun onCreate(savedInstanceState: Bundle?) {
super<AppCompatActivity>.onCreate(savedInstanceState)
Expand All @@ -102,6 +105,7 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback
initPoiLayer()
initAutoCompleteAdapter()
initFindMeButton()
initreverseButton()
centerOnCurrentLocation()
presenter?.onRestoreViewState()
}
Expand Down Expand Up @@ -213,7 +217,6 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback
listView.setEmptyView(emptyView)
restoreCurrentSearchTerm()
}

return true
}

Expand Down Expand Up @@ -407,16 +410,11 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback

override fun showRoutePreview(feature: Feature) {
this.destination = feature
val simpleFeature = SimpleFeature.fromFeature(feature)
val location = LocationServices.FusedLocationApi?.getLastLocation();
if (location is Location) {
val start: DoubleArray = doubleArrayOf(location.getLatitude(), location.getLongitude())
val dest: DoubleArray = doubleArrayOf(simpleFeature.getLat(), simpleFeature.getLon())
getInitializedRouter().setLocation(start).setLocation(dest).setCallback(this).fetch()
}
route()
}

override fun success(route: Route?) {
this.route = route;
runOnUiThread({
getSupportActionBar()?.hide()
findViewById(R.id.route_preview).setVisibility(View.VISIBLE)
Expand Down Expand Up @@ -478,7 +476,6 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback
} catch (e: Exception) {
Toast.makeText(this@MainActivity, "No route found", Toast.LENGTH_LONG).show()
}

}

private fun getMarkerItem(icon: Int, loc: Location, place: MarkerItem.HotspotPlace): MarkerItem {
Expand All @@ -496,44 +493,68 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback
findViewById(R.id.route_preview).setVisibility(View.GONE)
}

fun route() {
val simpleFeature = SimpleFeature.fromFeature(destination)
val location = LocationServices.FusedLocationApi?.getLastLocation()
if (reverse) {
if (location is Location) {
val start: DoubleArray = doubleArrayOf(simpleFeature.getLat(), simpleFeature.getLon())
val dest: DoubleArray = doubleArrayOf(location.getLatitude(), location.getLongitude())
getInitializedRouter().setLocation(start).setLocation(dest).setCallback(this).fetch()
}
} else {
if (location is Location) {
val start: DoubleArray = doubleArrayOf(location.getLatitude(), location.getLongitude())
val dest: DoubleArray = doubleArrayOf(simpleFeature.getLat(), simpleFeature.getLon())
getInitializedRouter().setLocation(start).setLocation(dest).setCallback(this).fetch()
}
}
}

fun updateRoutePreview(destination: Feature?) {
val dest = SimpleFeature.fromFeature(destination)
(findViewById(R.id.by_car) as RadioButton).setOnCheckedChangeListener { compoundButton, b ->
if (b) {
val location = LocationServices.FusedLocationApi?.getLastLocation();
if (location is Location) {
val start: DoubleArray = doubleArrayOf(location.getLatitude(), location.getLongitude())
val dest: DoubleArray = doubleArrayOf(dest.getLat(), dest.getLon())
getInitializedRouter().setDriving().setLocation(start).setCallback(this).setLocation(dest).fetch();
}
type = Router.Type.DRIVING
route()
(findViewById(R.id.routing_circle) as ImageButton).setImageResource(R.drawable.ic_start_car_normal)
}
}
(findViewById(R.id.by_foot) as RadioButton).setOnCheckedChangeListener { compoundButton, b ->
if (b) {
val location = LocationServices.FusedLocationApi?.getLastLocation();
if (location is Location) {
val start: DoubleArray = doubleArrayOf(location.getLatitude(), location.getLongitude())
val dest: DoubleArray = doubleArrayOf(dest.getLat(), dest.getLon())
getInitializedRouter().setWalking().setLocation(start).setLocation(dest).setCallback(this).fetch();
}
type = Router.Type.WALKING
route()
(findViewById(R.id.routing_circle) as ImageButton).setImageResource(R.drawable.ic_start_walk_normal)
}
}
(findViewById(R.id.by_bike) as RadioButton).setOnCheckedChangeListener { compoundButton, b ->
if (b) {
val location = LocationServices.FusedLocationApi?.getLastLocation();
if (location is Location) {
val start: DoubleArray = doubleArrayOf(location.getLatitude(), location.getLongitude())
val dest: DoubleArray = doubleArrayOf(dest.getLat(), dest.getLon())
getInitializedRouter().setBiking().setLocation(start).setLocation(dest).setCallback(this).fetch()
}
type = Router.Type.BIKING
route()
(findViewById(R.id.routing_circle) as ImageButton).setImageResource(R.drawable.ic_start_bike_normal)
}
}
}

private fun reverse() {
reverse = !reverse;
(findViewById(R.id.route_preview) as RoutePreviewView).reverse = this.reverse
if(reverse) {
findViewById(R.id.starting_location_icon).setVisibility(View.GONE)
findViewById(R.id.destination_location_icon).setVisibility(View.VISIBLE)
} else {
findViewById(R.id.starting_location_icon).setVisibility(View.VISIBLE)
findViewById(R.id.destination_location_icon).setVisibility(View.GONE)
}
route()
}

private fun initreverseButton() {
(findViewById(R.id.route_reverse) as ImageButton).setOnClickListener({ reverse()})

(findViewById(R.id.routing_circle) as ImageButton).setOnClickListener (
{presenter?.onShowDirectionList()})
}

override fun onBackPressed() {
if ((findViewById(R.id.route_preview)).getVisibility() == View.VISIBLE) {
mapController?.getMap()?.layers()?.remove(path)
Expand All @@ -547,7 +568,31 @@ public class MainActivity : AppCompatActivity(), ViewController, Router.Callback
finish()
}

override fun showDirectionList() {
val instructionStrings = ArrayList<String>()
val instructionType= ArrayList<Int>()
val instructionDistance= ArrayList<Int>()
for(instruction in route!!.getRouteInstructions() ) {
instructionStrings.add(instruction.getHumanTurnInstruction())
instructionType.add(instruction.turnInstruction)
instructionDistance.add(instruction.distance)
}
val simpleFeature = SimpleFeature.fromFeature(destination)
val intent = Intent(this, javaClass<InstructionListActivity>())
intent.putExtra("instruction_strings", instructionStrings)
intent.putExtra("instruction_types", instructionType)
intent.putExtra("instruction_distances", instructionDistance)
intent.putExtra("destination", simpleFeature.toString())
intent.putExtra("reverse", this.reverse)
startActivityForResult(intent, requestCodeSearchResults)
}

private fun getInitializedRouter(): Router {
return Router().setApiKey(BuildConfig.VALHALLA_API_KEY);
when(type) {
Router.Type.DRIVING -> return Router().setApiKey(BuildConfig.VALHALLA_API_KEY).setDriving()
Router.Type.WALKING -> return Router().setApiKey(BuildConfig.VALHALLA_API_KEY).setWalking()
Router.Type.BIKING -> return Router().setApiKey(BuildConfig.VALHALLA_API_KEY).setBiking()
}
}
}

13 changes: 11 additions & 2 deletions app/src/main/kotlin/com/mapzen/erasermap/view/RoutePreviewView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ import com.mapzen.valhalla.Router


public class RoutePreviewView : RelativeLayout {
public var reverse : Boolean = false
public var destination: SimpleFeature? = null
set (destination) {
(findViewById(R.id.destination) as TextView).setText(destination?.getTitle())
if(reverse) {
(findViewById(R.id.starting_point) as TextView).setText(destination?.getTitle())
} else {
(findViewById(R.id.destination) as TextView).setText(destination?.getTitle())
}
}

public var route: Route? = null
set (route) {
(findViewById(R.id.starting_point) as TextView).setText(R.string.current_location)
if(reverse) {
(findViewById(R.id.destination) as TextView).setText(R.string.current_location)
} else {
(findViewById(R.id.starting_point) as TextView).setText(R.string.current_location)
}
}

public constructor(context: Context) : super(context) {
Expand Down
Loading