[m2]新增全局路径测试数据工具类

This commit is contained in:
chenfufeng
2023-03-14 19:41:19 +08:00
parent a80a9d3c78
commit 6cdfe94c79
2 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1 @@
全局路径规划的GSON格式的轨迹点数据放这里然后使用PlanningDataUtils工具类解析一下

View File

@@ -0,0 +1,50 @@
package com.mogo.eagle.core.function.utils
import android.content.Context
import mogo.telematics.pad.MessagePad
import org.json.JSONArray
import org.json.JSONException
import java.io.IOException
import java.io.InputStream
object PlanningDataUtils {
@JvmStatic
fun test(context: Context?): List<MessagePad.Location> {
if (context == null) return emptyList()
val list: MutableList<MessagePad.Location> = ArrayList()
val jsonStr = getAssetsString(context, "planningDataTest.txt")
try {
val jsonElements = JSONArray(jsonStr)
for (i in 0 until jsonElements.length()) {
val s = jsonElements.getJSONObject(i)
val builder = MessagePad.Location.newBuilder()
builder.latitude = s.getDouble("latitude")
builder.longitude = s.getDouble("longitude")
list.add(builder.build())
}
} catch (e: JSONException) {
e.printStackTrace()
}
return list
}
private fun getAssetsString(context: Context, fileName: String): String {
var buffer: ByteArray? = null
var istream: InputStream? = null
try {
istream = context.resources.assets.open(fileName)
buffer = ByteArray(istream.available())
istream.read(buffer)
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
istream?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
return String(buffer!!)
}
}