如何在Android中使用形状文件显示地图?

时间:2016-11-29 10:10:22

标签: android google-maps shapefile

我尝试使用以下代码从SD卡读取并显示shapefile

    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();
    }

这是我的app build.gradle文件详细信息

 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'

}

最后我得到空黑屏

任何人都可以帮我解决这个问题吗?我在过去三天尝试这个例子

2 个答案:

答案 0 :(得分:1)

最后我找到了使用Openmap库的答案

以下是使用Android中的Openmap.jar读取和显示形状文件的步骤和示例屏幕。

1)下载样本形状文件zip(我使用了印度形状文件)

2)解压缩zip文件并选择一个以.shp

结尾的文件

3)将.shp文件添加到设备存储中并获取该文件位置

4)将该文件位置分配给OpenMap库" ShapeFile"班级(第一级)

5)" ShapeFile" class转换此数据并存储为" ESRIRecord"班级(二级)

6)最后使用" ESRIRecord"我们获得了PolygonOptions x和y点,这些点指定在Google Map上显示形状(第三级)

关于步骤:   #1,#2和#3步骤将随着不同类型的文件读取而改变。 例如:从我们的应用程序中,我们可以从服务器下载所需的zip文件,并将该文件解压缩并存储在设备位置(或) 我们可以将所需的zip文件存储在项目级别,然后将该文件解压缩并存储在设备位置等。

      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);
    }

enter image description here

答案 1 :(得分:0)