LoginSignup
0
0

More than 1 year has passed since last update.

【Android】locationフィールドにGeoPointが使えなかった時の対処法

Posted at

はじめに

次のように、locationフィールドに対してGeoPointのインスタンスを設定して、データを追加しようとしました。

val data = hashMapOf(
    "createdAt" to FieldValue.serverTimestamp(),
    "location" to GeoPoint(0.0, 0.0),
)

val collectionReference = firestore
    .collection("records")
    .document(recordId)

val reference = collectionReference
    .add(data)
    .await()

ところが、上記の処理を実行してみるとエラーが発生しました。

Found conflicting getters for name getDefaultInstanceForType on class com.google.type.LatLng

エラーの理由

これは、GeoPointはクラスであり、シリアライズとデシリアライズが不可能なためこのようなエラーが発生するということを表しています。

なので、次のようなルールを設定することはできません。

function isValidRecord(record) {
    return record.size() == 2
      && "createdAt" in record && record.createdAt is timestamp
      && "location" in record && (record.location == null || record.location is latlng)
}

ところがどっこいの解決方法

ところが、いろいろ試していくとフィールド名にlocationという名前を設定していたことが原因だということがわかりました。

なので、locationからpositionというフィールド名に変えてGeoPoint(0.0, 0.0)を渡してみると、なんとエラーを発生させることなく成功しました。

val data = hashMapOf(
    "createdAt" to FieldValue.serverTimestamp(),
    "position" to GeoPoint(0.0, 0.0),
)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0