KVISH
KVISH

Reputation: 13178

MapBox showing a map with a pin

I'm using MapBox and showing a pin where the user's geocode is set. The program works fine on all apis...except 19 where it's a black screen. I'm not sure what to make of it as I followed the documentation here:

https://www.mapbox.com/android-sdk/examples/marker/

https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/annotations/CustomInfoWindowActivity.java

I have some of my code below:

Gradle:

compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:4.2.0-beta.1@aar'){
    transitive=true
}
compile('com.mapbox.mapboxsdk:mapbox-android-services:1.2.1@aar') {
    transitive = true
}

Getting the mapView

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.setStyle(Style.MAPBOX_STREETS);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);

Call back when map is ready:

public void onMapReady(MapboxMap mapboxMap) {
    // Relevant sources shown only
    mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);
    mapboxMap.setOnInfoWindowClickListener(this);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(geocodeLatLng)
            .zoom(15)
            .bearing(0)
            .build();
    mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    Marker position = mapboxMap.addMarker(new MarkerOptions().position(geocodeLatLng).title(location));
    position.setSnippet(address);
    position.showInfoWindow(mapboxMap, mapView);
}

Layout file:

<?xml version="1.0" encoding="utf-8"?>

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

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:style_url="mapbox://styles/mapbox/streets-v9"
        mapbox:zoom="11"/>


</RelativeLayout>

Is there anything else I'm missing? I have the AndroidManifest.xml and gradle changes as well. It clearly works in newer API's just not the older ones. The website says they support from API 15+?

Upvotes: 0

Views: 1375

Answers (1)

KVISH
KVISH

Reputation: 13178

Not sure why but this fixed it. Previously I was doing this in my MapFragmentActivity at the very top:

MapboxAccountManager.start(this, getString(R.string.access_token));

Turns out (reading their documentation carefully) that it should be done on app load in the MainActivity.java. I did that and it worked. For some reason it works if you do this in the class in question before setContentView(R.layout.activity_main); but only on newer devices (not sure the distinction).

Upvotes: 1

Related Questions