Search code examples
androidandroid-fragmentsshapefileopenmap

Can't get map fragment when trying to initialise the Map to render shapefile using OpenMap library


I have been trying to display a shapefile using openmap library. On click of a button on first activity, the application goes to the second activity (which is also the second page) but then it suddenly crashes.

I ran the debugger and realised that mapFragment variable was still null, it was getting the value from findFragmentById. I ultimately intend to display a shapefile on the second page of my application.

    public class ShapeFileParser extends AppCompatActivity implements OnMapReadyCallback {

    GoogleMap mMap;
    ShapeFile shapeFile;
    File file;

    public void setUpMap() {
        try {
            org.apache.commons.io.FileUtils.copyInputStreamToFile((ShapeFileParser.this.getAssets().open("healthsites.shp")), file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            shapeFile = new ShapeFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            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)));
                        }
                        mMap.addPolygon(polygonOptions);
                        Log.v("myapp","polygon added");
                    }

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

            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeMap();
    }

    private void initializeMap() {
        if (mMap == null) {
            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            // mapFrag is null on checking with the debugger
            mapFrag.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        setUpMap();
    }
}

This is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ShapeFileParser" >
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

I am just trying an example to render a shape file onto the second page but I feel I am going no where since last two days. Kindly suggest some input if any.

I am a newbie in android dev, please be patient with me. Many thanks!


Solution

  • The OpenMap Library is used for Java Swing and JFrame applications and can't be used in an Android application as far as I can tell. The Google Map SDK has everything you need to display Maps in an Android Application including getting device location, displaying Places info, setting map markers, polylines, and polygons. Getting location updates in the backgroud, etc... To set it up, create a new Maps Activity from the Gallery. It will generate all the needed files for you and give you a list of instructions to get an API Key so that Maps can be used in your app. Here is a question explaining the security of your api key that I answered a while back. Google Maps Docs explains all the basic functionality of how to get Maps setup. I would recommend you visit some YouTube Tutorials on how to set Maps up in your Android application.

    <--EDIT-->

    Here is an answer I found on Stack Overflow from about two years ago. It doesn't really use the OpenMap Libray, but it's a workaround. You can pull ShapeFile data and use it in Google Maps Polygon, Polyline, and LatLng classes and display those on a map in your android application.