[m2]新增全局路径测试数据工具类
This commit is contained in:
@@ -0,0 +1 @@
|
||||
全局路径规划的GSON格式的轨迹点数据放这里,然后使用PlanningDataUtils工具类解析一下
|
||||
@@ -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!!)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user