Storing LatLng using Android Realm

前端 未结 2 465
忘了有多久
忘了有多久 2021-01-25 03:43

What is there cleaner whay of storing a RealmObject that contains a Google\'s LatLng object int Realm?

Area {
Latlat coord;
String name
}
相关标签:
2条回答
  • 2021-01-25 04:02

    You can create Model like this which is in java :

    public class ActionDataModel extends RealmObject
    {
      private LocationDataModel targetLatLong;
    
      @NonNull
      public LatLng getTargetLatLong()
      {
        return new LatLng(targetLatLong.getLatitude(), targetLatLong.getLongitude());
      }
    
      public void setTargetLatLong(@NonNull LatLng targetLatLong)
      {
        this.targetLatLong = new LocationDataModel(targetLatLong);
      }
    
    }
    

    Here is the parent model which is in kotlin :

    open class LocationDataModel(latLng: LatLng? = null)
    : RealmObject() {
      var latitude = latLng?.latitude
      var longitude = latLng?.longitude
    }
    
    0 讨论(0)
  • 2021-01-25 04:17

    Although it might seem natural to save the data as an object

    LatLng { 
     double lat; 
     double lng; 
    }
    

    remember that Realm is an object store - if you create an object, it will be stored in a table (entity). As suggested by Mohammed Ra (in the comment above), you should store an object which contains the fields latitude and longitude, both as double

    class Area extends RealmObject {
      double lat; 
      double lng;
      String name;
      ...
    }
    

    This solution also improves the performance of queries involving the coordinates - in which case you should put @Index annotation on them.

    0 讨论(0)
提交回复
热议问题