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

Add ability to display markers on the map #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions frontend/interchange.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<orderEntry type="library" name="kotlin-react" level="project" />
<orderEntry type="library" name="kotlin-react-dom" level="project" />
<orderEntry type="library" name="kotlinx-html-js" level="project" />
<orderEntry type="library" name="kotlinx-coroutines-core" level="project" />
</component>
</module>

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"kotlinx-coroutines-core": "^1.3.2",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-map-gl": "^5.1.2"
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/app/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Marker {
transform: translate(-50%, -50%);
font-size: 25px;
color: #f08300;
}
80 changes: 73 additions & 7 deletions frontend/src/app/App.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,72 @@
package app

import Marker
import logo.logo
import react.*
import react.dom.*
import search.search
import ReactMapGL
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.await
import kotlinx.coroutines.launch
import kotlin.browser.window

interface ViewportState: RState {
var width: String
var height: String
var latitude: Number
var longitude: Number
var zoom: Number
var waypointsResult: WaypointsResult
}

typealias Location = Array<Number>

interface Waypoint {
val name: String
val location: Location
}

interface Intersection {
val name: String
val classifications: Array<String>
val location: Location
}

interface Route {
val duration: Number
val distance: Number
val intersections: Array<Intersection>
}

interface WaypointsResult {
val waypoints: Array<Waypoint>
val routes: Array<Route>
}

class EmptyWaypointsResult : WaypointsResult {
override val waypoints: Array<Waypoint> = emptyArray()
override val routes: Array<Route> = emptyArray()
}


class App : RComponent<RProps, ViewportState>() {
init {
state.width = "100vw"
state.height = "100vh"
state.latitude = 52.132633
state.longitude = 5.291266
state.zoom = 7
}
override fun ViewportState.init() {
width = "100vw"
height = "100vh"
latitude = 52.132633
longitude = 5.291266
zoom = 11
waypointsResult = EmptyWaypointsResult()

val mainScope = MainScope()
mainScope.launch {
val result = fetchWaypointsResult()
setState {
waypointsResult = result
}
}
}

override fun RBuilder.render() {
div {
Expand All @@ -39,6 +82,7 @@ class App : RComponent<RProps, ViewportState>() {
longitude = state.longitude
zoom = state.zoom
onViewportChange = { viewport ->
console.log(viewport)
setState {
width = viewport.width
height = viewport.height
Expand All @@ -48,9 +92,31 @@ class App : RComponent<RProps, ViewportState>() {
}
}
}

for (route in state.waypointsResult.routes) {
for (intersection in route.intersections) {
Marker {
attrs {
longitude = intersection.location[0]
latitude = intersection.location[1]
}

div("Marker") {
+"🍿"
}
}
}
}
}
}

suspend fun fetchWaypointsResult(): WaypointsResult {
val responsePromise = window.fetch("https://api.myjson.com/bins/19rdz4")
val response = responsePromise.await()
val jsonPromise = response.json()
val json = jsonPromise.await()
return json.unsafeCast<WaypointsResult>()
}
}

fun RBuilder.app() = child(App::class) {}
1 change: 1 addition & 0 deletions frontend/src/logo/Logo.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
font-size: 80px;
text-align: right;
line-height: 55px;
text-shadow: 0 0 10px rgba(0,0,0,.2);
}

.Small {
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/map/react-map-gl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ external interface ReactMapGLProps : RProps {
var zoom: Number
var onViewportChange: (ReactMapGLViewport) -> Unit
}

@JsName("Marker")
external val Marker : RClass<MarkerProps>

external interface MarkerProps : RProps {
var longitude: Number
var latitude: Number
}
4 changes: 2 additions & 2 deletions frontend/src/search/Search.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ interface SearchState: RState {

class Search(props: SearchProps): RComponent<SearchProps, SearchState>(props) {
init {
state.from = ""
state.to = ""
state.from = "Codestar"
state.to = "Codestarwars"
}

override fun RBuilder.render() {
Expand Down