Wednesday, March 6, 2013

Android Draw Multiple polygon on Map overlay

Android Draw Multiple polygon on  Map overlay



Below I wrote the code for how to draw a multiple polygon on Google map overlay.
I hope it will help full for you.


Screen Shot



Activity Source code


package com.example.androidtest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;

/**
 * @author vijayakumar
 */

public class AndroidprojectMApActivity extends MapActivity {
       MapView myMapView = null;
       MapController myMC = null;
       GeoPoint geoPoint = null;
        GeoPoint gP1;
       private List<Overlay> mapOverlays;
     PathOverlay pathOverlay = null;
       private Projection projection;
        List<GeoPoint>  path = new ArrayList<GeoPoint>();
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myMapView = (MapView) findViewById(R.id.mapview);
        myMapView.setBuiltInZoomControls(true);
          mapOverlays = myMapView.getOverlays();       
          projection = myMapView.getProjection();
          MapController mapController = myMapView.getController();
          mapController.setZoom(14);
          mapOverlays.add(new PathOverlay());
       
       }

       @Override
       protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
       }

      }


Overlay Source Code
package com.example.androidtest; 
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

class PathOverlay extends Overlay{
       Point startPoint = null, endPoint = null;
       Path path  = null;
       GeoPoint gPointA;
       GeoPoint gPointA1;
       Point pointA ;
       Point pointA1 ;
      //private final List<GeoPoint> _points;
  
    public void draw(Canvas canvas, MapView mapv, boolean shadow){
        super.draw(canvas, mapv, shadow);
        Projection projection = mapv.getProjection();
        Paint   mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.parseColor("#FF0000"));
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.BEVEL);
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        mPaint.setStrokeWidth(2);
          InputStream is = mapv.getResources().openRawResource(R.raw.json);
          byte[] buffer;
            try {
                  buffer = new byte[is.available()];
                    while (is.read(buffer) != -1);
                      String jsontext = new String(buffer);
                      JSONObject  fromobj3 = null;
                      JSONObject  fromobj4 = null;
                      JSONObject  fromobj5 = null;
                      JSONObject jObject2 =  new JSONObject(jsontext);
                      JSONObject  fromobj = jObject2.getJSONObject("kml");
                      JSONObject  fromobj1 = fromobj.getJSONObject("document");
                      JSONObject  fromobj2 = fromobj1.getJSONObject("folder");
                      JSONArray entries = fromobj2.getJSONArray("placemark");
                      int i;
                      for (i=0;i<entries.length();i++)
                      {
                            JSONObject post = entries.getJSONObject(i);
                            fromobj3 = post.getJSONObject("polygon");
                            fromobj4 = fromobj3.getJSONObject("outerboundaryis");
                            fromobj5 = fromobj4.getJSONObject("linearring");
                            String  coordinates =   fromobj5.getString("coordinates").toString();
                           
                                String[] coor = coordinates.split("0.00 ");
                                path = new Path();
                                             for(int j = 0; j<coor.length;j++) {
                                                       String latlong[] = coor[j].substring(0, coor[j].length()-1).split(",");
                                                         Double lon = Double.valueOf(latlong[0].toString());
                                                         Double lat = Double.valueOf(latlong[1].toString());
                                                         gPointA = new GeoPoint((int) (lat * 1E6), (int)((lon) * 1E6));
                                                            pointA = new Point();
                                                            projection.toPixels(gPointA, pointA);
                                                            Log.d("greenzone", "Geo Points   ::    " +gPointA.getLatitudeE6()  +" :: " + gPointA.getLongitudeE6());
                                                            if (j == 0) { //This is the start point
                                                             path.moveTo(pointA.x,pointA.y);
                                                            }
                                                            else {
                                                            if (j == coor.length - 1)// This is the link last point to first point
                                                               {
                                                             path.lineTo(pointA.x, pointA.y);
                                                                }
                                                            else// this is the first and last middle points
                                                               {
                                                             path.lineTo(pointA.x, pointA.y);
                                                                }
                                                               }
                                                    }
                                             canvas.drawPath(path, mPaint);
                                                  }
                   
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
     
    }
   
}

Download Source Code

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...