0

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!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jyotirmaya ojha
  • 73
  • 1
  • 13
  • Can you post the error log? Is it throwing an NPE? Also, do you have an api key to display maps? And finally, are you swapping fragments or are you launching a whole new activity to display your map. – J. Jefferson Jul 25 '18 at 14:48
  • @J.Jefferson The application just crashes, I hadnt put a try catch around the mapFrag but then debugger showed that it was still null even after the assignment statement was executed. – jyotirmaya ojha Jul 25 '18 at 14:54
  • Alright. I edited my first comment to ask more questions. But did you obtain an api key from the google api console to display your map? – J. Jefferson Jul 25 '18 at 14:55
  • 1
    Also, I just looked up the OpenMap Library. It's used for Java Swing clients and not the Android Framework. I don't think you can use OpenMap with Android. Google Maps has all the support you need to use Maps in an android application. – J. Jefferson Jul 25 '18 at 15:04
  • @J.Jefferson Oh geez! My bad. Thanks for saving my time. By the way Sir, I have already tried OSMdroid and Mapforge but they have their own formats, cant use shapefiles. I cant use ARCgis because of crazy restriction of this challenge I am taking part in. Any idea which library you think I should explore. The idea is to display bunch of coordinates and shape files in an amdroid app. – jyotirmaya ojha Jul 25 '18 at 15:21
  • I edited my answer. I've never used ShapeFile before, but the link I provided in the edit along with the Google Maps Docs link should get your started. – J. Jefferson Jul 25 '18 at 15:49

1 Answers1

1

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.

J. Jefferson
  • 980
  • 8
  • 12
  • Okay, I will explore Google Maps SDK, hopefully it would help me to plot shapefiles in android. Google Maps uses its own KML format, not shapefiles. But thanks again. Your reply did prevent me from going into wrong direction. – jyotirmaya ojha Jul 25 '18 at 15:24
  • No problem. All the links posted helped me a ton in figuring out how to setup maps. It's not difficult once you have the basics. And if this answer helped, please mark it accepted. Good luck. – J. Jefferson Jul 25 '18 at 15:28
  • 1
    I have been following the same code, trying to render the file. Thanks anyway! PS: I didn't accept your answer in case I get a better answer by tonight! I will accept yours in case yours the best. Cheers! – jyotirmaya ojha Jul 25 '18 at 15:51