-
-
Save vehdox/a9a093b51dfc0c763b3a60896ed9defe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapAdapter { | |
var map: MapboxMap? = null | |
private val cmds: Queue<MapboxMap.() -> Unit> = LinkedBlockingQueue() | |
fun attachMap(map: MapboxMap) { | |
this.map = map | |
val bounds = LatLngBounds.Builder() | |
.include(LatLng(56.0431162,37.5329372)) // Northeast | |
.include(LatLng(55.4725803, 37.7006232)) | |
.include(LatLng(55.6368534, 37.4429595)) | |
.include(LatLng(55.7499363, 37.9918402))// Southwest | |
.build() | |
map.setLatLngBoundsForCameraTarget(bounds) | |
Log.e("map", ""+map) | |
while (cmds.isNotEmpty()) { | |
map.let(cmds.poll()) | |
} | |
} | |
fun offer(cmd: MapboxMap.() -> Unit) { | |
val map = map | |
if (map == null) { | |
cmds.offer(cmd) | |
return | |
} | |
map.let(cmd) | |
} | |
fun detachMap() { | |
this.map = null | |
this.cmds.clear() | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetRoute{ | |
fun getRoute(origin: Point, destination: Point) { | |
val builder = NavigationRoute.builder() | |
.accessToken(Mapbox.getAccessToken()) | |
.profile(DirectionsCriteria.PROFILE_WALKING) | |
.origin(origin) | |
.destination(destination) | |
builder.build() | |
.getRoute(object : Callback<DirectionsResponse> { | |
override fun onFailure(call: Call<DirectionsResponse>?, t: Throwable) { | |
Log.e(FeedbackBottomSheet.TAG, "Error: " + t.printStackTrace()); | |
} | |
override fun onResponse(call: Call<DirectionsResponse>?, response: Response<DirectionsResponse>?) { | |
Log.d(FeedbackBottomSheet.TAG, "Response code: " + response!!.code()); | |
if (response.body() == null) { | |
Log.e(FeedbackBottomSheet.TAG, "No routes found, make sure you set the right user and access token.") | |
return | |
} else if (response.body()!!.routes().size < 1) { | |
Log.e(FeedbackBottomSheet.TAG, "No routes found") | |
return | |
} | |
val currentRoute: DirectionsRoute = response.body()!!.routes().get(0) | |
MapPresenter().drawPolyline(currentRoute) | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapFragment: Fragment(), com.example.vendox.citytrack.Presentation.View.Map.MapView, PermissionsListener, LocationEngineListener { | |
private lateinit var mapView: MapView | |
lateinit var presenter: MapPresenter | |
// variables for adding location layer | |
private var permissionsManager: PermissionsManager? = null | |
private var locationPlugin: LocationLayerPlugin? = null | |
private var locationEngine: LocationEngine? = null | |
private var originLocation: Location? = null | |
private var originPosition: Point? = null | |
private var destinationPosition: Point? = null | |
private val adapter = MapAdapter() | |
companion object { | |
fun newInstance(): MapFragment { | |
return MapFragment() | |
} | |
} | |
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, | |
savedInstanceState: Bundle?): View? { | |
super.onCreateView(inflater, container, savedInstanceState) | |
val rootView = inflater?.inflate(R.layout.map_box_fragment, container, false) | |
Mapbox.getInstance(activity, getString(R.string.access_token)) | |
mapView = rootView!!.findViewById(R.id.mapView) | |
mapView.onCreate(savedInstanceState) | |
var i = 0 | |
presenter = MapPresenter() | |
val startButton: Button = rootView.findViewById(R.id.startButton) | |
startButton.setOnClickListener(View.OnClickListener { | |
Log.i("text", ""+ startButton.text) | |
if (i > 0 ){ | |
stopTracking() | |
startButton.text = "Start" | |
i = 0 | |
} else { | |
startTracking() | |
startButton.text = "Stop" | |
i += 1 | |
} | |
}) | |
mapView.getMapAsync (adapter::attachMap) | |
enableLocationPlugin() | |
return rootView | |
} | |
@SuppressWarnings("MissingPermission") | |
override fun onStart() { | |
super.onStart() | |
locationEngine?.requestLocationUpdates() | |
locationPlugin?.onStart() | |
mapView.onStart() | |
} | |
override fun onResume() { | |
super.onResume() | |
mapView.onResume() | |
} | |
override fun onPause() { | |
super.onPause() | |
mapView.onPause() | |
} | |
override fun onStop() { | |
super.onStop() | |
mapView.onStop() | |
} | |
override fun onLowMemory() { | |
super.onLowMemory() | |
mapView.onLowMemory() | |
} | |
override fun onDestroyView() { | |
super.onDestroyView() | |
mapView.onDestroy() | |
} | |
override fun onSaveInstanceState(outState: Bundle) { | |
super.onSaveInstanceState(outState) | |
mapView.onSaveInstanceState(outState) | |
} | |
@SuppressLint("MissingPermission") | |
override fun enableLocationPlugin() { | |
if (PermissionsManager.areLocationPermissionsGranted(activity)){ | |
initializeLocationEngine() | |
adapter.offer { | |
locationPlugin = adapter.map!!.let { LocationLayerPlugin(mapView, it, locationEngine) } | |
adapter.offer { | |
locationPlugin?.setLocationLayerEnabled(LocationLayerMode.TRACKING) | |
} | |
} | |
} else { | |
permissionsManager = PermissionsManager(this) | |
permissionsManager?.requestLocationPermissions(activity) | |
} | |
} | |
@SuppressLint("MissingPermission") | |
override fun initializeLocationEngine() { | |
locationEngine = LostLocationEngine(activity) | |
locationEngine?.priority = LocationEnginePriority.HIGH_ACCURACY | |
locationEngine?.activate() | |
val lastLocation = locationEngine?.lastLocation | |
if (lastLocation != null) { | |
originLocation = lastLocation | |
setCameraPosition(lastLocation) | |
} else { | |
locationEngine?.addLocationEngineListener(this) | |
} | |
} | |
override fun setCameraPosition(location: Location) { | |
adapter.offer { | |
adapter.map?.animateCamera(CameraUpdateFactory.newLatLngZoom( | |
LatLng(location.latitude, location.longitude), 13.0))} | |
} | |
override fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<out String>?,@NonNull grantResults: IntArray?) { | |
permissionsManager?.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
} | |
override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) { | |
} | |
override fun onPermissionResult(granted: Boolean) { | |
if (granted) { | |
enableLocationPlugin() | |
}else{ | |
activity.finish() | |
} | |
} | |
@SuppressWarnings("MissingPermission") | |
override fun onConnected() { | |
locationEngine?.requestLocationUpdates() | |
} | |
override fun onLocationChanged(location: Location?) { | |
if (location != null){ | |
originLocation = location | |
setCameraPosition(location) | |
locationEngine?.removeLocationEngineListener(this) | |
Log.e("Mapbox", "" +location.latitude + location.longitude ) | |
} | |
} | |
fun startTracking(){ | |
val way = arrayListOf<Point>() | |
var i = 0 | |
val mLocTrackingInterval: Long = 500 // 0.5 sec | |
val trackingDistance: Float = 0F; | |
val trackingAccuracy: LocationAccuracy = LocationAccuracy.HIGH; | |
val builder= LocationParams.Builder() | |
.setAccuracy(trackingAccuracy) | |
.setDistance(trackingDistance) | |
.setInterval(mLocTrackingInterval) | |
SmartLocation.with(activity) | |
.location() | |
.continuous() | |
.config(builder.build()) | |
.start(object : OnLocationUpdatedListener { | |
override fun onLocationUpdated(location: Location) { | |
originLocation = location | |
way.add(Point.fromLngLat(originLocation!!.longitude, originLocation!!.latitude)) | |
if (way.size > 1){ | |
val ind: Int = way.lastIndex | |
originPosition = way[ind -1] | |
destinationPosition = way[ind] | |
GetRoute().getRoute(originPosition!!, destinationPosition!!) | |
} | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapPresenter { | |
private val adapter = MapAdapter() | |
fun drawPolyline(currentRoute: DirectionsRoute) { | |
val latLngs = arrayListOf<LatLng>() | |
val point: MutableList<Point> | |
val lineString: LineString = LineString.fromPolyline(currentRoute.geometry()!!, Constants.PRECISION_6) | |
point = lineString.coordinates() | |
for (p in 0..point.size - 1) { | |
latLngs.add(LatLng(point[p].latitude(), point[p].longitude())) | |
} | |
Log.e("latlngs123", "" + point) | |
adapter.offer { | |
addPolyline( | |
PolylineOptions() | |
.addAll(latLngs) | |
.color(Color.parseColor("#3bb2d0")) | |
.width(8F)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment