Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make GEO Point Class Parcelable or Serializable as I want to pass them using Intent

I am using Cloud Firestore as my Database and in every document there is a Map named restaurant_info that has as it typically stores <"name","Name of The Restaurant">, <"Location",GEOPoint of the Restaurant> and some more fields. The problem is the Geo Point class doesn't implement Parcelable or Serializable interface.

like image 217
Himanshu Rawat Avatar asked Jan 02 '23 14:01

Himanshu Rawat


1 Answers

There are two ways in which you can solve this. The first one would be to add to your Restaurant class that implements the Parcelable interface:

class Restaurant implements Parcelable {}

And then override writeToParcel() method and create another constructor like this:

private GeoPoint geoPoint;

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeDouble(geoPoint.getLatitude());
    parcel.writeDouble(geoPoint.getLongitude());
}

public Restaurant(Parcel in) {
    Double lat = in.readDouble();
    Double lng = in.readDouble();
    geoPoint = new GeoPoint(lat, lng);
}

The second approach would be to store the lat and long in your Restaurant class as double primitves and everytime you need a GeoPoint object, create it like this:

GeoPoint geoPoint = new GeoPoint(restaurant.getLatitude(), restaurant.getLongitudeE6());
like image 129
Alex Mamo Avatar answered Jan 29 '23 15:01

Alex Mamo