0

i am working with google maps on android studio, in which i draw polygon on map. till this everything going great but now i wanted to store this polygon to sqlite database and then retrieve this data to listView. now i don't understand how to implement this in my code. if anyone have any idea about this then please save me.

here is my code where i draw polygon:

     @Override
public void onMapReady(final GoogleMap googleMap) {
    final List<LatLng> latLngList = new ArrayList<>(); // list of polygons
    final List<Marker> markerList = new ArrayList<>();
    mMap = googleMap;


    LatLng center = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
    final MarkerOptions markerOptions = new MarkerOptions().position(center).title(center.latitude + ":" + center.longitude);
    mMap.clear();
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(center));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(center, 18));
    googleMap.addMarker(markerOptions);

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(final LatLng latLng) {
            int height = 50;
            int width = 30;
            BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.mipmap.marker);
            Bitmap b = bitmapdraw.getBitmap();
            Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
            /*Marker marker = googleMap.addMarker(markerOptions);*/
            Marker marker = mMap.addMarker(new MarkerOptions().position(latLng).draggable(true).icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
            markerList.add(marker);
            latLngList.add(latLng);

            drawPolygon(latLngList);
            polygon.setClickable(true);
        }
    });

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
            polygon.remove();
            /*marker.remove();
            markerList.remove(marker);*/

        }
    });
}

private void drawPolygon(List<LatLng> latLngList) {

    if (polygon != null) {
        polygon.remove();
    }

    PolygonOptions polygonOptions = new PolygonOptions();
    polygonOptions.fillColor(0x7F228B22);
    polygonOptions.strokeColor(Color.GREEN);
    polygonOptions.strokeWidth(5);
    polygonOptions.addAll(latLngList);
    polygon = mMap.addPolygon(polygonOptions);
}

my listview will look like [![this][1]][1]

Mrunal
  • 578
  • 2
  • 21
  • 39
  • 1
    _"store this polygon to sqlite database and then retrieve this data to listView"_ How would it be displayed in the ListView? As a list of coordinates? Or graphically as a polygon? You have the coordinates in your code anyway. Store those to SQLite. If you don't know how to use SQLite, then have a look at the documentation and tutorials and try something. – Markus Kauppinen Mar 26 '20 at 07:36
  • 1
    @MarkusKauppinen i want to store my polygon in kml file format but i don't know exactly how to do that – Mrunal Mar 26 '20 at 10:06
  • Why would you store it as a KML? And what do you mean by *in kml file format*? You have all the coords that make your Polygon already, as mentioned in the previous comment. Why would you not store them directly? – MrUpsidown Mar 26 '20 at 12:27
  • 1
    KML is just XML and XML is just text and text can be stored into SQLite. So the initial problems are a) writing the coordinates into a `String` that conforms to the KML specification and b) writing the `String` to SQLite. And then the opposite steps would be done later. You'll probably find some guides/tutorials/libraries for writing XML in Java. And using SQLite should be well documented. (Though Google's documentation will recommend using `Room` instead.) XML parsing (reading) should be well supported on Android. – Markus Kauppinen Mar 26 '20 at 14:51
  • @MarkusKauppinen ok. so which method is good, storing KML or storing polygon co-ordinates to sqlite. and one thing i want to clear is that, i like to show polygon graphically in my list view – Mrunal Mar 26 '20 at 15:30
  • You are already drawing polygons on a Google map. You are doing it by providing a `List` of `LatLng` objects. I don't see how KML would help here. Your problem (if it is still is a one) is how to serialize that list to a format accepted by SQLite as a `String` or a `Blob` and then deserialize it back to a `List`. That must be a common thing that people do (no matter what the object type in the list), so you'll probably find something. Or maybe SQLite isn't a great choice for this use case(?) I don't know, actually. – Markus Kauppinen Mar 28 '20 at 16:00
  • have you find any solution ? – Mrunal Apr 03 '20 at 16:52
  • @MarkusKauppinen can you share small piece of code how to do serialize and deserialize the List ? – Mrunal Apr 03 '20 at 17:00
  • I don't remember if I've ever even used SQLite. But maybe one option would be to [use GSON](https://stackoverflow.com/questions/14228912/how-to-convert-list-to-json-in-java) to make the list a `String` (of JSON), store it and then use GSON again to get the `List` object back from the JSON string. – Markus Kauppinen Apr 03 '20 at 17:20

1 Answers1

1

You could save principal attributes you want. For example... in my case I needed to save coordinates LatLng and camera zoom. Then you can make a custom class like this.

class CustomLatLng : Serializable{

    var latitudeList : MutableList<String> = mutableListOf()
    var longitudeList : MutableList<String> = mutableListOf()
    var cameraPositionLat : Double = 0.0
    var cameraPositionLng : Double = 0.0
    var zoomCamera : Float = 12f

    fun add(latitude : Double, longitude : Double ){
        latitudeList.add(latitude.toString())
        longitudeList.add(longitude.toString())
    }
    fun clearLatLng(){
        latitudeList.clear()
        longitudeList.clear()
    }

    fun isNotEmpty(): Boolean{
        return !(latitudeList.isEmpty() || longitudeList.isEmpty())
    }

}

// in other place of your code put this...

const val PARA_NAME_POLYGONS = "polygons"
fun savePolygon(poligons:CustomLatLng ) {

    val editor = sharedPreferences.edit()
    editor.putString(PARA_NAME_POLYGONS, serializePolygon(poligons))
    editor.apply()
    //editor.commit()

}

fun cleanPolygon() {
    val editor = sharedPreferences.edit()
    editor.remove(PARA_NAME_POLYGONS)
    editor.apply()
}

private fun serializePolygon(polygon: CustomLatLng): String {

    val gson = Gson()
    return gson.toJson(polygon)

}

fun getPolygon():CustomLatLng {

    val gson = Gson()
    val json =   sharedPreferences.getString(PARA_NAME_POLYGONS,"").toString()

    if (json.isEmpty())
        return CustomLatLng()

    return gson.fromJson(json,CustomLatLng::class.java)
}

wherever you have a polygon , save only data that you will need to recreate a Polygon

 val customLatLng = CustomLatLng()
polygon?.points?.forEach {
    customLatLng.add(it.latitude, it.longitude)
}

customLatLng.zoomCamera = 10f

savePolygon(customLatLng)

note I'm saving only data I need, You must create as many variables as you need, I hope it helps

jaitor
  • 109
  • 1
  • 3