如何在Android中使用形状文件显示地图? [英] How to display a map using shape files in Android?

查看:92
本文介绍了如何在Android中使用形状文件显示地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的应用程序build.gradle文件的详细信息

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'

}

最后我黑屏了

有人可以帮助我吗?我正在尝试过去三天的示例

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

推荐答案

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

Finally I find the answer using Openmap library

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

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

1)下载示例形状文件zip(我使用过印度形状文件)

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

2)解压缩该zip文件,然后选择一个以.shp结尾的文件

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

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

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

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

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

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

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

6)最后使用"ESRIRecord"获得PolygonOptions x和y点,这些点分配给在Google Map(第三级)上显示形状

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

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

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

这篇关于如何在Android中使用形状文件显示地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆