无法使用 @TypeConverter 在 LatLng 和 String 之间进行转换以存储在房间数据库中
我正在构建一个 Android 应用程序,需要在房间数据库中存储纬度和经度。
在我的内部,我使用 LatLng 类作为 Google Maps SDK 的一部分,但需要在 Room 中存储经度和纬度数据。
理想情况下,我想将数据保存在单列中,这就是为什么我使用 typeconvertor 将 LatLng 转换为字符串
由于 Room 无法直接存储 LatLng 对象,我希望使用 @TypeConverter 将数据作为字符串存储在 Room 数据库中。
不幸的是,出于某种原因,Room 不允许我在 LatLng 上使用 TypeConverter,如下面的错误所示:
构建失败
类型转换器的返回类型无效。 - stringToLatLng(java.lang.String)
当然,只要我提供将 LatLng 转换为 Room 接受并再次返回的类型的函数,它就应该允许这样做吗?我还尝试返回字符串和 LatLng 的可为空类型
感谢您帮助善良的互联网陌生人
Database.kt
@TypeConverters(Converters::class)
@Database(entities = [MyEntity::class], version = 1)
abstract class MyDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
Converters.kt
class Converters{
@TypeConverter
fun latLngToString(latLng: LatLng) : String{
return "(${latLng.latitude},${latLng.longitude}"
}
@TypeConverter
fun stringToLatLng(string: String) : LatLng{
val s = string.replace("(", "").replace(")", "")
val list = s.split(",")
return LatLng(list.first().toDouble(), list.last().toDouble())
}
}
MyEntity.kt
@Entity
data class MyEntity(
@PrimaryKey
val iD: String,
var location: LatLng,
val timeStamp: Long
)
I'm building an android app that needs to store latitude and longitude in a room database.
Internally in my, am using the LatLng class as part of the Google Maps SDK, but need to store the longitude and latitude data in Room.
Ideally I want to save the data in a single column which is why I am using typeconvertor to convert LatLng to a String
As Room cannot store LatLng objects directly, I was hoping to use @TypeConverter to store the data as a string in the Room database.
Unfortunately Room is'nt letting me use the TypeConverter on LatLng for some reason, as shown in the error below:
Build Failed
Invalid return type for a type converter. - stringToLatLng(java.lang.String)
Surely so long as I provide functions to convert LatLng to a Type that Room accepts and back again it should allow this? I have also tried returning nullable types of both string and LatLng
Thanks for your help kind internet strangers
Database.kt
@TypeConverters(Converters::class)
@Database(entities = [MyEntity::class], version = 1)
abstract class MyDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
Converters.kt
class Converters{
@TypeConverter
fun latLngToString(latLng: LatLng) : String{
return "(${latLng.latitude},${latLng.longitude}"
}
@TypeConverter
fun stringToLatLng(string: String) : LatLng{
val s = string.replace("(", "").replace(")", "")
val list = s.split(",")
return LatLng(list.first().toDouble(), list.last().toDouble())
}
}
MyEntity.kt
@Entity
data class MyEntity(
@PrimaryKey
val iD: String,
var location: LatLng,
val timeStamp: Long
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您在其他答案中的评论,您的 TypeConverters 很好。我已经使用您的代码按原样测试了它们(除了输入的值可能无效)。因此,使用:-
然后使用:-
getAllMyEntities 是:-
结果日志产生:-
通过应用程序检查的数据库为:-
但是,当最初将代码应用到现有项目时,我遇到了 无效退货类型。
我所做的就是考虑使用替代的 MyLatLng (2 个双精度)和 TypeConverters 使用:-
import com.google.android.gms.maps.model.LatLng
和 TypeConverters :-
将位置更改为MyLatLng 而不是 LatLng 编译得很好,所以我然后添加了将 MyLatLng 转换为 LatLng 的函数,然后编译得很好。
然后我恢复到原来的 LatLng 并注释掉 MyLatLng 转换器,然后NO invalid return type失败。
然后,我仅使用您的代码和附加内容创建了一个全新的项目,以使其能够运行。它编译正常并运行(如上面的结果所示)。
新项目中使用的完整代码(带有导入):-
MyEntity
Converters
AllDao
TheDatabase
主要活动
As per your comment in the other answer, your TypeConverters are fine. I have tested them using your code as is (except that that values input are perhaps invalid). So using:-
and then using :-
With getAllMyEntities being:-
The resultant log produces:-
The database, via App Inspection is:-
HOWEVER, when initially applying your code to an existing project I had the issue with the invalid return type.
What I did was to then look at using an alternative MyLatLng (2 doubles) and TypeConverters using:-
import com.google.android.gms.maps.model.LatLng
and TypeConverters :-
Changing location to be a MyLatLng rather than LatLng compiled fine, so I then added the function to convert a MyLatLng to a LatLng, this then compiled fine.
I then reverted to the original LatLng and commented out the MyLatLng converters and then NO invalid return type failure.
I then created a brand new project just with your code and extras to enable it to be run. It compiled OK and ran (as the results above show).
The full code used in the new project (with the imports):-
MyEntity
Converters
AllDao
TheDatabase
MainActivity
您需要将 LatLng 存储到 String 有什么具体原因吗?就我而言,我只是用双纬度和经度保存它。
Is there any specific reason that you need to store LatLng to String? In my case I just save it with double Latitude and Longitude.