1
votes

Please Help me, I have stored my lat, long value into arrayList<String>, but I'm not able to add it to new LatLng class.

I have to fetch Lat, Long value by json and add it to (String) ArrayList<String>.

I want to add my all ArrayList Lat, Long into latlng class, where i can see it using polyline.

But i'm able to get one lat and long and it shows in my google map. But i'm not able to get all ArrayList value. I tried many times. And How to Measure Distance By Lat and Long Below is my code;

ArrayList<String> arr;

 if(b!=null){
           arr = (ArrayList<String>)b.getStringArrayList("array_list");
           // System.out.println(arr);
        }
    String s=arr.toString();
    System.out.println(s.substring(1, s.length()-1));

    String ss=s.substring(1,s.length()-1);
    System.out.println(ss);
    d=ss.split(",");
    for (int i=0;i<d.length;i++){
        if(i%2==0){
            dd=d[i];
            System.out.println(dd +"all even");
        }else{
            nd=d[i];

            System.out.println(nd +"all odd");
        }


    }

    List<LatLng> point = new ArrayList<LatLng>();
    double pointY[]={Double.parseDouble(nd)};
    double pointX[]={Double.parseDouble(dd)};
    for (int i = 0 ; i < pointX.length; i++){

        point.add(new LatLng(pointX[i],pointY[i]));
    }
2
may i ask why you dont use a json framework for parsing the json into a pojo ? I suggest a framework called jacksonStefan Höltker
Dear sir, I'm able to get all json and i have stored it into arraylist & pass it's data using putExtra. But my problem is i'm not able to add arraylist value into my google map. I'm only able to get one marker. But i have 5 lat, long value.user7234752
Instead of doing all this weird string manipulation create a custom arraylist of modal class and add data to your list using either constructor or getter/setter. After that loop through the list and get your dataVivek Mishra

2 Answers

2
votes

Try this,

your pointY, pointX were initialized with last item. Put those in for loop to fill all the values

ArrayList<String> arr;
   Bundle b = getIntent().getExtras();
 if(b!=null){
           arr = (ArrayList<String>)b.getStringArrayList("array_list");
           // System.out.println(arr);
        }
    String s=arr.toString();
    System.out.println(s.substring(1, s.length()-1));

String ss=s.substring(1,s.length()-1);
System.out.println(ss);


d=ss.split(",");
 List<Double> pointY = new ArrayList<>();
    List<Double> pointX = new ArrayList<>();

for (int i=0;i<d.length;i++){
    if(i%2==0){
        dd=d[i];
        pointX.add(Double.parseDouble(dd));
        System.out.println(dd +"all even");
    }else{
        nd=d[i];
        pointY.add(Double.parseDouble(nd));
        System.out.println(nd +"all odd");
    }


}

List<LatLng> point = new ArrayList<LatLng>();
for (int i = 0 ; i < pointX.size(); i++){

    point.add(new LatLng(pointX.get(i),pointY.get(i)));
}
0
votes

I am not sure what and why you are trying to do and your code is not readable(sorry your coding style is horrible). But I can show a snippet about how I would do this.

public static void main(String args[]) {
    // some example data
    String coord1lat = "15.235235";
    String coord1lng = "31.776776";
    String coord2lat = "34.334346";
    String coord2lng = "31.776776";
    String[] coordinatesStringList = {coord1lat, coord1lng, coord2lat, coord2lng};

    List<LatLng> latLngList = new ArrayList<LatLng>();
    for(int i=0; i<coordinatesStringList.length; i+=2){
        LatLng pont = new LatLng(Double.parseDouble(coordinatesStringList[i]), Double.parseDouble(coordinatesStringList[i+1]));
        latLngList.add(point);
    }
}

Again, I highly discourage you to store latitudes and longitudes in one array. I don't know your case but try to store them in a couples at least. Like String coord1 = "15.23235,16.232342"