Made MapFragment work again, introduced new system to handle access token, updated MapFragmentActivity example

This commit is contained in:
Tobrun
2015-10-07 14:32:56 +02:00
committed by Brad Leege
parent 209d20664c
commit e673a26788
8 changed files with 57 additions and 25 deletions

View File

@@ -37,6 +37,10 @@
android:name="io.fabric.ApiKey"
android:value="9724157045ff7d083492c6d9ae03e60e8609d461" />
<meta-data
android:name="com.mapbox.accessToken"
android:value="" />
</application>
</manifest>

View File

@@ -41,15 +41,7 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
}
mMapView = (MapView) findViewById(R.id.secondMapView);
// Load the access token
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.token)));
String line = reader.readLine();
mMapView.setAccessToken(line);
} catch (IOException e) {
Log.e(TAG, "Error loading access token from token.txt: " + e.toString());
}
mMapView.setAccessToken(Util.getAccessToken(this));
mMapView.onCreate(savedInstanceState);
mMapView.setStyleUrl(MapView.StyleUrls.EMERALD);

View File

@@ -286,6 +286,9 @@ public class MainActivity extends AppCompatActivity {
startActivity(new Intent(getApplicationContext(), InfoWindowAdapterActivity.class));
return true;
case R.id.action_map_fragment:
startActivity(new Intent(getApplicationContext(), MapFragmentActivity.class));
return true;
/*
case R.id.followNone:
mMapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.NONE);

View File

@@ -9,6 +9,7 @@ import android.view.ViewGroup;
import com.mapbox.mapboxsdk.views.MapView;
// TODO move this to SDK
public class MapFragment extends Fragment {
//
@@ -36,7 +37,10 @@ public class MapFragment extends Fragment {
Log.v(TAG, "onCreateView");
// Create the map
mMap = (MapView) inflater.inflate(R.layout.fragment_main, container, true);
mMap = (MapView) inflater.inflate(R.layout.fragment_main, container, false);
// Set accessToken
mMap.setAccessToken(Util.getAccessToken(container.getContext()));
// Need to pass on any saved state to the map
mMap.onCreate(savedInstanceState);

View File

@@ -1,11 +1,12 @@
package com.mapbox.mapboxsdk.testapp;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;
public class MapFragmentActivity extends AppCompatActivity {
@@ -32,11 +33,16 @@ public class MapFragmentActivity extends AppCompatActivity {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Context context = view.getContext();
MapView map = getMap();
map.setAccessToken(Util.getAccessToken(context));
map.setStyleUrl(MapView.StyleUrls.EMERALD);
MapView mapView = getMap();
mapView.setStyleUrl(MapView.StyleUrls.EMERALD);
mapView.setZoomLevel(12);
mapView.setCenterCoordinate(new LatLng(50.853658, 4.352419));
// move attribution control to right of screen
mapView.setAttributionGravity(Gravity.BOTTOM | Gravity.END);
int tenDp = (int) getResources().getDimension(R.dimen.attr_margin);
mapView.setAttributionMargins(0, 0, tenDp, tenDp);
}
}
}

View File

@@ -1,6 +1,8 @@
package com.mapbox.mapboxsdk.testapp;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;
@@ -62,14 +64,35 @@ public class Util {
return sb.toString();
}
public static String getAccessToken(final Context context){
// Load the access token
// TODO move this to SDK
public static String getAccessToken(Context context) {
String accessToken = getReleaseAccessToken(context);
if (TextUtils.isEmpty(accessToken)) {
accessToken = getDevelopmentAccessToken(context);
}
// TODO add validation + throw exception
return accessToken;
}
// TODO move this to SDK
private static String getReleaseAccessToken(Context context){
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return appInfo.metaData.getString("com.mapbox.accessToken");
} catch (Exception e) {
Log.e(TAG, "Failed to load AccessToken: " + e.getMessage());
return null;
}
}
// TODO move this to SDK
private static String getDevelopmentAccessToken(Context context) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.token)));
return reader.readLine();
} catch (IOException e) {
Log.e(TAG, "Error loading access token from token.txt: " + e.toString());
}catch (IOException e){
return null;
}
return "";
}
}

View File

@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<com.mapbox.mapboxsdk.views.MapView 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
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="fab_margin">16dp</dimen>
<dimen name="attr_margin">10dp</dimen>
</resources>