Skip to content
This repository has been archived by the owner on Jun 25, 2019. It is now read-only.

Encoding and ToolbarActions improvement #93

Open
wants to merge 9 commits 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
7 changes: 4 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ group 'com.apptreesoftware.mapview'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.2.50'
ext.kotlin_version = '1.2.51'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down Expand Up @@ -45,6 +45,7 @@ android {

dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.maps.android:android-maps-utils:0.5'
}
46 changes: 44 additions & 2 deletions android/src/main/kotlin/com/apptreesoftware/mapview/MapActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.google.android.gms.maps.model.*

class MapActivity : AppCompatActivity(),
OnMapReadyCallback {
var menu: Menu? = null
var googleMap: GoogleMap? = null
var markerIdLookup = HashMap<String, Marker>()
var polylineIdLookup = HashMap<String, Polyline>()
Expand Down Expand Up @@ -122,13 +123,54 @@ class MapActivity : AppCompatActivity(),
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
this.menu = menu
MapViewPlugin.toolbarActions.forEach {
val item = menu.add(0, it.identifier, 0, it.title)
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
addMenuItem(it)
}
return super.onCreateOptionsMenu(menu)
}

fun addMenuItem(toolbarAction: ToolbarAction) {
if (this.menu != null) {
val menuItem = menu?.findItem(toolbarAction.identifier)
if (menuItem == null) {
val item = menu?.add(0, toolbarAction.identifier, 0, toolbarAction.title)
if (toolbarAction.showIfRoom) {
item?.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
} else {
item?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
}
}
}

fun editMenuItem(toolbarAction: ToolbarAction) {
if (this.menu != null) {
val menuItem = menu?.findItem(toolbarAction.identifier)
if (menuItem != null) {
menuItem.title = toolbarAction.title
if (toolbarAction.showIfRoom) {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
} else {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
}
}
}

fun removeMenuItem(id: Int) {
if (this.menu != null) {
menu?.removeItem(id)
}
}

fun setMenuItemVisibility(id: Int, visible: Boolean) {
if (this.menu != null) {
val menuItem = menu?.findItem(id)
if (menuItem != null)
menuItem.setVisible(visible)
}
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
MapViewPlugin.handleToolbarAction(item.itemId)
Expand Down
30 changes: 18 additions & 12 deletions android/src/main/kotlin/com/apptreesoftware/mapview/MapPolygon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.apptreesoftware.mapview

import com.google.android.gms.maps.model.LatLng

open class MapPolygon(val identifier: String, val points: ArrayList<LatLng>, val holes: ArrayList<Hole>, val strokeWidth: Float,
open class MapPolygon(val identifier: String, val points: List<LatLng>, val holes: ArrayList<Hole>, val strokeWidth: Float,
val fillColor: Int, val strokeColor: Int, val jointType: Int) {
companion object {
fun fromMap(map: Map<String, Any>): MapPolygon? {
Expand All @@ -16,16 +16,9 @@ open class MapPolygon(val identifier: String, val points: ArrayList<LatLng>, val
pointsList.add(LatLng(latitude, longitude))
}
val holesParam = map["holes"] as ArrayList<Map<String, Map<String, Any>>>
for (hole in holesParam) {
val holePointsMap = hole["points"] as ArrayList<Map<String, Any>>
val holePoints = arrayListOf<LatLng>()
holePointsMap.forEach {
val latitude: Double = it["latitude"] as Double
val longitude: Double = it["longitude"] as Double
holePoints.add(LatLng(latitude, longitude))
}
if (holePoints.isNotEmpty())
holesList.add(Hole(holePoints))
for (holeMap in holesParam) {
val hole = Hole.fromMap(holeMap)
holesList.add(hole)
}
val fillColorMap = map["fillColor"] as Map<String, Int>
val strokeColorMap = map["strokeColor"] as Map<String, Int>
Expand All @@ -38,5 +31,18 @@ open class MapPolygon(val identifier: String, val points: ArrayList<LatLng>, val
}
}

class Hole(val points: ArrayList<LatLng>)
class Hole(val points: List<LatLng>) {
companion object {
fun fromMap(map: Map<String, Map<String, Any>>): Hole {
val pointsList = arrayListOf<LatLng>();
val pointsParam = map["points"] as List<Map<String, Any>>
pointsParam.forEach {
val latitude: Double = it["latitude"] as Double
val longitude: Double = it["longitude"] as Double
pointsList.add(LatLng(latitude, longitude))
}
return Hole(pointsList)
}
}
}

Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.apptreesoftware.mapview

import android.graphics.Color
import android.util.Log
import com.google.android.gms.maps.model.JointType
import com.google.android.gms.maps.model.LatLng

open class MapPolyline(val identifier: String, val points: ArrayList<LatLng>, val width: Float,
open class MapPolyline(val identifier: String, val points: List<LatLng>, val width: Float,
val color: Int, val jointType: Int) {
companion object {
fun fromMap(map: Map<String, Any>): MapPolyline? {
val identifier = map["id"] as String
val pointsList = arrayListOf<LatLng>();
val pointsParam = map["points"] as ArrayList<Map<String, Any>>
pointsParam.forEach {
val points = map["points"] as List<Map<String, Any>>
points.forEach {
val latitude: Double = it["latitude"] as Double
val longitude: Double = it["longitude"] as Double
pointsList.add(LatLng(latitude, longitude))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.IndoorBuilding
import com.google.android.gms.maps.model.IndoorLevel
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.PolyUtil
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
Expand Down Expand Up @@ -163,6 +164,20 @@ class MapViewPlugin(val activity: Activity) : MethodCallHandler {
return assetManager.openFd(key)
}

fun encodePath(points: List<LatLng>): String {
var result = ""
if (points.isNotEmpty())
result = PolyUtil.encode(points)
return result
}

fun decodePath(encodedPath: String): List<LatLng> {
var result = listOf<LatLng>()
if (encodedPath.isNotEmpty())
result = PolyUtil.decode(encodedPath)
return result
}

fun indoorBuildingActivated(indoorBuilding: IndoorBuilding?) {
if (indoorBuilding == null) {
this.channel.invokeMethod("indoorBuildingActivated", null)
Expand All @@ -173,7 +188,7 @@ class MapViewPlugin(val activity: Activity) : MethodCallHandler {
"levels" to mappingIndoorLevels(indoorBuilding.levels)))
}
}

fun indoorLevelActivated(level: IndoorLevel?) {
if (level == null) {
this.channel.invokeMethod("indoorLevelActivated", null)
Expand All @@ -195,7 +210,7 @@ class MapViewPlugin(val activity: Activity) : MethodCallHandler {
)
}
}

override fun onMethodCall(call: MethodCall, result: Result): Unit {
when {
call.method == "setApiKey" -> result.success(false)
Expand Down Expand Up @@ -224,6 +239,31 @@ class MapViewPlugin(val activity: Activity) : MethodCallHandler {
result.success(true)
return
}
call.method == "addToolbarAction" -> {
val map = call.arguments as Map<String, Any>
val toolbarAction = ToolbarAction(map)
mapActivity?.addMenuItem(toolbarAction)
result.success(true)
}
call.method == "editToolbarAction" -> {
val map = call.arguments as Map<String, Any>
val toolbarAction = ToolbarAction(map)
mapActivity?.editMenuItem(toolbarAction)
result.success(true)
}
call.method == "removeToolbarAction" -> {
val map = call.arguments as Map<String, Any>
val id = map["identifier"] as Int
mapActivity?.removeMenuItem(id)
result.success(true)
}
call.method == "setToolbarActionVisibility" -> {
val map = call.arguments as Map<String, Any>
val id = map["identifier"] as Int
val visible = map["visible"] as Boolean
mapActivity?.setMenuItemVisibility(id, visible)
result.success(true)
}
call.method == "dismiss" -> {
mapActivity?.finish()
result.success(true)
Expand Down Expand Up @@ -312,6 +352,20 @@ class MapViewPlugin(val activity: Activity) : MethodCallHandler {
call.method == "removePolygon" -> {
handleRemovePolygon(call.arguments as Map<String, Any>)
}
call.method == "encodePath" -> {
val encodedPath = handleEncodePath(call.arguments as Map<String, Any>)
result.success(encodedPath)
}
call.method == "decodePath" -> {
val decodedPath = handleDecodePath(call.arguments as Map<String, Any>)
val list = arrayListOf<Map<String, Any>>()
decodedPath?.forEach {
list.add(mapOf(
"latitude" to it.latitude,
"longitude" to it.longitude))
}
result.success(list)
}
else -> result.notImplemented()
}
}
Expand Down Expand Up @@ -411,5 +465,21 @@ class MapViewPlugin(val activity: Activity) : MethodCallHandler {
mapActivity?.removePolygon(it)
}
}

fun handleEncodePath(map: Map<String, Any>): String? {
val pointsList = map["points"] as List<Map<String, Any>>
val points = arrayListOf<LatLng>()
pointsList.forEach {
val lat = it["latitude"] as Double
val lng = it["longitude"] as Double
points.add(LatLng(lat, lng))
}
return encodePath(points)
}

fun handleDecodePath(map: Map<String, Any>): List<LatLng>? {
val encodedPath = map["encodedPath"] as String
return decodePath(encodedPath)
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.apptreesoftware.mapview

data class ToolbarAction(val title : String, val identifier: Int) {
class ToolbarAction(val title: String, val identifier: Int, val showIfRoom: Boolean) {
constructor(map: Map<String, Any>)
: this(title = map["title"] as String,
identifier = map["identifier"] as Int)
: this(title = map["title"] as String,
identifier = map["identifier"] as Int,
showIfRoom = map["showIfRoom"] as Boolean)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ToolbarAction) return false

if (identifier != other.identifier) return false

return true
}

override fun hashCode(): Int {
return identifier
}

}
Loading