Question

I want to add polygon on map.here i am retrieving lat lng values from arraylist.I am trying to add a polygon from these lat lng values .But the polygon not added on map.please tell me where i made a mistake

my code

  polarl=(ArrayList<HashMap<String, String>>) 
            getIntent().getSerializableExtra("polMyList");
    Log.e("POL ARRAY", polarl.toString());
    Polygon polygon;
    LatLng polLatLng;
    if(polarl.size()>0){
        for(int k=0;k<polarl.size();k++){
            String polLat =polarl.get(k).get("polLat").toString();
             String polLng =polarl.get(k).get("polLng").toString();
             if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
                 double HPollat = Double.parseDouble(polLat.trim());
                 double HPolLong= Double.parseDouble(polLng.trim());
                 polLatLng=new LatLng(HPollat, HPolLong);

                 Log.e("POL LAT LANG", ""+polLatLng);
                 rectOptions = new         
  PolygonOptions().add(polLatLng).fillColor(Color.BLUE).strokeColor(Color.RED);
 Polygon polygon1 = _googleMap.addPolygon(rectOptions);
     }
        }
        } 
Was it helpful?

Solution

Try this way

 Polygon polygon;
 LatLng polLatLng;

PolygonOptions rectOptions=new PolygonOptions();

if(polarl.size()>0){
    for(int k=0;k<polarl.size();k++){
        String polLat =polarl.get(k).get("polLat").toString();
         String polLng =polarl.get(k).get("polLng").toString();
         if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
             double HPollat = Double.parseDouble(polLat.trim());
             double HPolLong= Double.parseDouble(polLng.trim());
             polLatLng=new LatLng(HPollat, HPolLong);

             Log.e("POL LAT LANG", ""+polLatLng);

           rectOptions.add(polLatLng);

   }
    }
  }
   Polygon polygon1 = _googleMap.addPolygon(rectOptions.strokeColor(Color.RED).fillColor(Color.BLUE));

For more information go to this SO POST

OTHER TIPS

Try this code,

 ArrayList<LatLng> points= new ArrayList<LatLng>();
    polarl=(ArrayList<HashMap<String, String>>)getIntent().getSerializableExtra("polMyList");
    LatLng polLatLng;
    if(polarl.size()>0){
        for(int k=0;k<polarl.size();k++){
            String polLat =polarl.get(k).get("polLat").toString();
             String polLng =polarl.get(k).get("polLng").toString();
             if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
                 double HPollat = Double.parseDouble(polLat.trim());
                 double HPolLong= Double.parseDouble(polLng.trim());
                 polLatLng=new LatLng(HPollat, HPolLong);
                 points.add(polLatLng);
             }
        }
    }
    polygonOptions = new PolygonOptions();
    polygonOptions.fillColor(Color.TRANSPARENT);
    polygonOptions.strokeColor(Color.RED);
    polygonOptions.strokeWidth(3);
    polylineOptions.color(Color.RED);
    polylineOptions.width(3);
    polylineOptions.addAll(points);
    map.addPolyline(polylineOptions);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top