1

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]));
    }
  • 1
    may i ask why you dont use a json framework for parsing the json into a pojo ? I suggest a framework called jackson – Stefan Höltker Dec 27 '17 at 10:20
  • 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. –  Dec 27 '17 at 10:23
  • 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 data – Vivek Mishra Dec 27 '17 at 10:53

2 Answers2

2

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)));
}
Raghavendra
  • 2,305
  • 25
  • 30
  • getting initialize is not allow here pointX[x++]={Double.parseDouble(dd)}; –  Dec 27 '17 at 10:26
  • 1
    wow.. Thank U so much, now I'm able to get 2 polyline :) .. Thank U Sir, –  Dec 27 '17 at 10:41
  • @HarishKumar glad! that helped – Raghavendra Dec 27 '17 at 10:43
  • Give me 2 mints i'm adding 2 another lat, longs thank you sir –  Dec 27 '17 at 10:44
  • Dear sir, I have another query. How can i calculate my all distance? –  Dec 28 '17 at 07:12
  • @HarishKumar try the solution available here https://stackoverflow.com/questions/6981916/how-to-calculate-distance-between-two-locations-using-their-longitude-and-latitu – Raghavendra Dec 28 '17 at 07:15
  • Dear sir, i have asked it on stackoverflow please see it :. https://stackoverflow.com/questions/48003275/how-to-calculate-distance-using-lat-and-lon –  Dec 28 '17 at 07:20
  • I'm able to calculate one and last lat lon distance. –  Dec 28 '17 at 07:30
  • Thank U sir, I got solution i had to sum all of value. –  Dec 28 '17 at 07:59
0

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"

  • Dear sir, i have arraylist like = 28.6613494,77.1483808, 28.6613494,77.1483808, 28.550819, 77.255524, 28.6613486,77.1483728, 28.6613486,77.1483728, 28.6613486,77.1483728. how can i added it. if i would not splite it. –  Dec 27 '17 at 11:02
  • I'm not sure what are you trying to say. You don't have to split it if it's an array. You would've splitted that if you would have all the coordinates in on String(not string array). Like `String coordinates = "28.6613494,77.1483808, 28.6613494,77.1483808, 28.550819, 77.255524, 28.6613486,77.1483728, 28.6613486,77.1483728, 28.6613486,77.1483728" ` But it's not like that, is it? – Farkhad Gojazadeh Dec 27 '17 at 11:05
  • oh. I understood your logic. Thank You. –  Dec 27 '17 at 11:07
  • And please name you variables a little more appropriate. 'dd' and 'ss' don't tell much about your code and you will get crazy soon trying to debug something you've written couple of weeks ago – Farkhad Gojazadeh Dec 27 '17 at 11:11
  • I'm not able to use it. Please Give full solution with arraylist. I'm using ArrayList not Array. –  Dec 27 '17 at 11:12