How to store the LatLng variable in Android in an SQL Lite Database for use later?

14,779

Solution 1

I solved it by doing this:

LatLng latLng;
Double l1=latlng.latitude;
Double l2=latlng.longitude;
String coordl1 = l1.toString();
String coordl2 = l2.toString();
l1 = Double.parseDouble(coordl1);
l2 = Double.parseDouble(coordl2);


googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(l1, l2))
                    .title(title)
                    .snippet(info));

So from a Latlng variable to two doubles, to strings,back to doubles and passed to newmarker position and it works!

:)

Solution 2

i save lat and long seperate, maybe this is also a solution for you

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

Solution 3

As an alternative you could store the position in the SharedPreferences. Here is the TypedPreferences library and the LatLngPreference helper class to do this easily.

Share:
14,779
deucalion0
Author by

deucalion0

I am a student studying Computing Science.

Updated on June 27, 2022

Comments

  • deucalion0
    deucalion0 almost 2 years

    I am trying to store marker information in an SQL Lite database on an Android device, I am currently converting the LatLng variable to a String and saving that way.

    LatLng latLng
    coords = latLng.toString();
    

    The problem is, and I have tried many things, is converting the string back to a latLng variable so it can be used again.

    Looking into the LatLng variable I see it is two doubles separated by a comma, I was thinking about splitting the string and converting to doubles then passing that as a Latlng but I don't know how to do this in Android.

    Any ideas on how to do this?