0

I'm attempting to add a map programmatically, as a fragment. Unfortunately, I haven't had much success. I am also attempting to add markers from a GeoJSTON file, which is stored locally.

The following is my main activity:

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get an instance of FragmentTransaction from your Activity
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        //add a fragment
        MapActivity myFragment = new MapActivity();
        fragmentTransaction.add(R.id.FragmentContainer, myFragment);
        fragmentTransaction.commit();

    }

Here's the XML for the main activity:

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout

android:id="@+id/FragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

    </FrameLayout>

</LinearLayout>

Here's the MapActivity class:

public class MapActivity extends Fragment {

    private MapView mv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_map, container, false);

        // Setup Map
        mv = (MapView) view.findViewById(R.id.mapview);
        mv.setCenter(new LatLng(-9.133248925209045,38.70980094773454));
        mv.setZoom(14);

        return view;
    }


    @Override
    public void onResume() {
        super.onResume();

        try {
            FeatureCollection features = DataLoadingUtils.loadGeoJSONFromAssets(getActivity(), "lisbon.geojson");
            ArrayList<Object> uiObjects = DataLoadingUtils.createUIObjectsFromGeoJSONObjects(features, null);

            for (Object obj : uiObjects) {
                if (obj instanceof Marker) {
                    mv.addMarker((Marker) obj);
                } else if (obj instanceof PathOverlay) {
                    mv.getOverlays().add((PathOverlay) obj);
                }
            }
            if (uiObjects.size() > 0) {
                mv.invalidate();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And the XML for the Map activity:

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.views.MapView
    android:id="@+id/mapview"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    mapbox:mapid="@string/mapLisbon"
    mapbox:accessToken="@string/testAccessToken"/>

 </FrameLayout>

Warnings/errors log:

W/System.err at com.mapbox.mapboxsdk.tileprovider.tilesource.TileJsonTileLayer$RetrieveJSONTask.doInBackground(TileJsonTileLayer.java:162)
W/System.err﹕ at com.mapbox.mapboxsdk.tileprovider.tilesource.TileJsonTileLayer$RetrieveJSONTask.doInBackground(TileJsonTileLayer.java:156)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
W/System.err﹕ ... 22 more
W/ViewRootImpl﹕ mView has no focus, use com.android.internal.policy.impl.PhoneWindow$DecorView{427aef70 V.E..... R.....ID 0,0-1080,1668} instead
Cœur
  • 37,241
  • 25
  • 195
  • 267
nunop
  • 2,036
  • 2
  • 20
  • 36

1 Answers1

0

Based on the stack trace if looks like the issue is that lisbon.geojson can't be found on the classpath at runtime. Is it located in src/main/assets directory?

Brad Leege
  • 481
  • 5
  • 7
  • Thank you so much for taking some of your time to answer. In fact, the lisbon.geojson wasn't in the right place when I originally posted this question, but it's now in the assets directory. However, all I keep getting is a blank user interface with a back button on the bottom. – nunop Apr 13 '15 at 23:19
  • You may want to try using `FrameLayout` instead of `LinearLayout` for the `@+id/FragmentContainer` – Brad Leege Apr 17 '15 at 21:28
  • No luck yet unfortunately. I've updated the errors/warnings log above, maybe it helps finding the problem. – nunop Apr 23 '15 at 22:57