How to display a map using shape files in Android?

北城以北 提交于 2019-12-13 09:47:21

问题


I have tried with below code for read and display shapefile from my SD card

    ShapefileFeatureTable shapefileFeatureTable = null;
    try {
        shapefileFeatureTable = new ShapefileFeatureTable(Environment.getExternalStorageDirectory().getAbsolutePath()+"/India_SHP/INDIA.shp");
        featureLayer = new FeatureLayer(shapefileFeatureTable);
        featureLayer.setRenderer(new SimpleRenderer(new SimpleMarkerSymbol(
                getResources().getColor(android.R.color.holo_blue_bright),
                28, SimpleMarkerSymbol.STYLE.CIRCLE)));

        mMapView.addLayer(featureLayer);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

And here is the my app build.gradle file details

 dependencies {
repositories {
    jcenter()
    // Add the Esri public Bintray Maven repository
    maven {
        url 'https://esri.bintray.com/arcgis'
    }
}
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'

compile 'com.esri.arcgis.android:arcgis-android:10.2.5'

}

And finally I am getting empty black screen

Can any one help me regarding this? I am trying this example from past three days


回答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.

      File file = new File(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;
        }

  ShapeFile shapeFile = new ShapeFile(targetFilePath);      

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

            if (shapeTypeStr.equals("POLYGON")) {
                // cast type after checking the type
                ESRIPolygonRecord polyRec = (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.ESRIFloatPoly poly = (ESRIPoly.ESRIFloatPoly)polyRec.polygons[i];

                    PolygonOptions polygonOptions = new PolygonOptions();
                    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(new LatLng(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);
    }



来源:https://stackoverflow.com/questions/40863110/how-to-display-a-map-using-shape-files-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!