Home > Enterprise >  Store Polygon points to sqlite database
Store Polygon points to sqlite database

Time:11-01

In my app I create fields on the map by using polygons.The polygons are created by the user by clicking on the map and creating points/markers and the clicking a button to create the polygon. Now I want these fields/polygons to be stored on my SQLite database but the points are given in lat/lng data type.

One way I thought of doing this is by storing the string of the polygon.getPoints() function:

Polygon p;
String latlng = p.getPoints().toString();

The output of latlng is:

[lat/lng: (-3.987413925092502,-18.76223847270012), lat/lng: (-4.878691546616136,-2.84572284668684), lat/lng: (-7.610723424687183,-11.666696257889273), lat/lng: (-3.987413925092502,-18.76223847270012)]

And then when retrieving it from the database convert all the points by first removing all the unnecessary text from the string using replaceAll() and then putting every single point on the list and then again converting them to lat/lng.

It seems a little bit like overkill is there a better way to handle this?

CodePudding user response:

Try this.

Pattern LAT_LNG = Pattern.compile("([-\\d.] ),([-\\d.] )");
String latlng = "[lat/lng: (-3.987413925092502,-18.76223847270012),"
      "lat/lng: (-4.878691546616136,-2.84572284668684),"
      "lat/lng: (-7.610723424687183,-11.666696257889273),"
      "lat/lng: (-3.987413925092502,-18.76223847270012)]";
Matcher matcher = LAT_LNG.matcher(latlng);
while (matcher.find()) {
    double lat = Double.parseDouble(matcher.group(1));
    double lng = Double.parseDouble(matcher.group(2));
    System.out.println("lat="   lat   " lng="   lng);
}

output:

lat=-3.987413925092502 lng=-18.76223847270012
lat=-4.878691546616136 lng=-2.84572284668684
lat=-7.610723424687183 lng=-11.666696257889273
lat=-3.987413925092502 lng=-18.76223847270012
  • Related