Skip to content Skip to sidebar Skip to footer

How To Display A Map Using Shape Files In Android?

I have tried with below code for read and display shapefile from my SD card ShapefileFeatureTable shapefileFeatureTable = null; try { shapefileFeatureTable = new Sh

Solution 1:

Finally I find the answer using Openmap library

Here is the steps and sample screens regarding shape file read and display using Openmap.jar in Android.

1) Download the sample shape file zip (I have used India shape file)

2) Extract the zip file and pick one file which ends with .shp

3) Add that .shp file in device storage and get that file location

4) Assign that file location to OpenMap library's "ShapeFile" class (First level)

5) The "ShapeFile" class convert this data and store as "ESRIRecord" class (Second level)

6) And finally using "ESRIRecord" we get PolygonOptions x and y points which assigns to display shape on Google Map (Third level)

Regarding steps : #1,#2 and #3 steps will change with different types of file reading. For example : From our app we can download the desire zip file from server and unzip and store that files in device location (or) We can store that desire zip file in project level then unzip and store that files in device location etc.

Filefile=newFile(getfile("INDIA.shp"));

        if (file.exists()) {
            Toast.makeText(getApplicationContext(), "File exists",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "File not exists @@@@@",
                    Toast.LENGTH_LONG).show();
            return;
        }

  ShapeFileshapeFile=newShapeFile(targetFilePath);      

      for (ESRIRecordesriRecord= shapeFile.getNextRecord(); esriRecord!=null;esriRecord = shapeFile.getNextRecord()){
            StringshapeTypeStr= ShapeUtils.getStringForType(esriRecord.getShapeType());
            Log.v("myapp","shape type = " + esriRecord.getRecordNumber() + "-" + shapeTypeStr);               

            if (shapeTypeStr.equals("POLYGON")) {
                // cast type after checking the typeESRIPolygonRecordpolyRec= (ESRIPolygonRecord)esriRecord;

                Log.v("myapp","number of polygon objects = " + polyRec.polygons.length);
                for (int i=0; i<polyRec.polygons.length; i++){
                    // read for a few layers
                    ESRIPoly.ESRIFloatPolypoly= (ESRIPoly.ESRIFloatPoly)polyRec.polygons[i];

                    PolygonOptionspolygonOptions=newPolygonOptions();
                    polygonOptions.strokeColor(Color.argb(150,200,0,0));
                    polygonOptions.fillColor(Color.argb(150,0,0,150));
                    polygonOptions.strokeWidth(2.0f);

                    Log.v("myapp","Points in the polygon = " + poly.nPoints);

                    for (int j=0; j<poly.nPoints; j++){
                        //Log.v("myapp",poly.getY(j) + "," + poly.getX(j));
                        polygonOptions.add(newLatLng(poly.getY(j), poly.getX(j)));
                    }
                    map.addPolygon(polygonOptions);
                    Log.v("myapp","polygon added");
                }

            }
            else {
                Log.v("myapp","error polygon not found (type = " + esriRecord.getShapeType() + ")");
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
        Log.v("myapp","error=" + e);
    }

enter image description here

Solution 2:

Post a Comment for "How To Display A Map Using Shape Files In Android?"