Offline tile caching using MapBox Android SDK - java

I have a working iOS prototype using the iOS tile-caching technique as shown below (Objective-C code):
RMTileCache * tileCache = [[RMTileCache alloc] initWithExpiryPeriod:0];
[tileCache setBackgroundCacheDelegate:self];
RMMapboxSource * tileSource = [[RMMapboxSource alloc] initWithMapID:mapID];
[tileCache beginBackgroundCacheForTileSource:tileSource southWest:southWest northEast:northEasth minZoom:minZoom maxZoom:maxZoom];
What this basically does is download the map, cache the tiles permanently and make it possible for the app to run offline in the future. Since we're going through the official payed API, this is of course not violating any of the legal restrictions.
Now I'd like to achieve the same on Android. I have the SDK running in Android Studio and a working project with a remote map using the Map ID, basically this (Android Eclipse layout XML):
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapid=“my_map_id" />
This works fine, but the solution has to be completely offline once the caching is done. My question is: is there a Java equivalent of the iOS source code above in the MapBox SDK? I attempted to look in the API, but could not find a solid reference to the tile caching system. And after some painful time trying to get it running based on the method names and code documentation, I gave up.
I'm running the latest GitHub distribution of MapBox along with the latest Android Studio, everything's up and running fine, but can't find the code to accomplish this. I don’t necessarily need an API reference, a few lines of code showing how it’s done would be enough.

Offline Tile Caching support is now available in the Mapbox Android SDK as of version 0.5.1. It was released on 20-December-2014. Here's a basic example of how to get started:
OfflineMapDownloader offlineMapDownloader = OfflineMapDownloader.getOfflineMapDownloader(getActivity());
BoundingBox boundingBox = mapView.getBoundingBox();
CoordinateSpan span = new CoordinateSpan(boundingBox.getLatitudeSpan(), boundingBox.getLongitudeSpan());
CoordinateRegion coordinateRegion = new CoordinateRegion(mapView.getCenter(), span);
offlineMapDownloader.beginDownloadingMapID("MapboxMapID", coordinateRegion, (int) mapView.getZoomLevel(), (int) mapView.getZoomLevel());
To load a previously saved map:
ArrayList<OfflineMapDatabase> offlineMapDatabases = offlineMapDownloader.getMutableOfflineMapDatabases();
OfflineMapDatabase db = offlineMapDatabases.get(0);
OfflineMapTileProvider tp = new OfflineMapTileProvider(getActivity(), db);
offlineMapOverlay = new TilesOverlay(tp);
mapView.addOverlay(offlineMapOverlay);

I asked this question to the support team, here's the answer:
"We don't currently have a release date for the Android SDK or for this feature, because both are in very early stages of development.
--
Tom MacWright
support#mapbox.com"
It's a very good product, I hope we can use it soon in Android.

In your layout file there must be:
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/yourMapViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
In code where you want to initialize MapView:
File file = new File("your full path to tiles db");
MBTilesLayer mbTilesLayer = new MBTilesLayer(file);
MapView mapView = (MapView) findViewById(R.id.yourMapViewId);
mapView.setTileSource(mbTilesLayer);

Related

Fusion Tables v2 POIs on Google Maps: Android example?

I'm looking for an example on how to display points of interest (saved in fusion tables) on a google map.
To do this in html and javascript it's quite trivial, check my javascript map with fusion tables example
See Fusion Tables page containing my POIs
My goal/question is how (need help in coding it) to achieve the same in an Android app. I'm new to android development and I already invested hours for the basics and checking documentation and examples.
Check this very good Google Maps example for Android I've found to get started (my test app is based on this code).
Fusion Tables v2 reference (points to google api client)
Google API Java client samples on github (most outdated: examples on v1)
So far I achieved to display a map centered on my last known location and to show a marker on it.
Because I couldn't find good examples for this, I decided to publish and share my findings, see: firepol / android-google-maps-fusion-tables on github
Now I'd like to show markers coming from fusion tables.
I'm stuck at executing the request, which I try to do via google api client.
Google API client example for Android
ActivityFeed feed = listActivities.execute();
Here my code (which I've put inside onCreate):
protected void prepareFusion() {
// Normally READONLY should be enough (see credential with one scope), but I checked online a console
// and I could see a public table only if I would grant both permissions
List<String> scopes = new ArrayList<>(Arrays.asList(FusiontablesScopes.FUSIONTABLES, FusiontablesScopes.FUSIONTABLES_READONLY));
credential = GoogleAccountCredential.usingOAuth2(this, scopes);
//credential = GoogleAccountCredential.usingOAuth2(this, Collections.singleton(FusiontablesScopes.FUSIONTABLES_READONLY));
// TODO : get account name automatically
// http://stackoverflow.com/questions/35789071/getting-the-gmail-id-of-the-user-in-android-6-0-marshmallow
credential.setSelectedAccountName("YOUR_GOOGLE_ACCOUNT");
client = new Fusiontables.Builder(
transport, jsonFactory, credential).setApplicationName("TestMap/1.0")
.build();
try {
String tableId = "1774o_WcrqSQlepLXlz1kgH_01NpCJ-6OyId9Pm1J";
Fusiontables.Query.Sql sql = client.query().sql("SELECT FileName,Name,Location FROM " + tableId);
//sql.execute();
//java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
Fusiontables.Table.Get table = client.table().get(tableId);
table.setFields("items(FileName,Name,Location)");
//table.execute();
// TODO : can't execute like this on main thread as the documentation example "suggests"
//https://developers.google.com/api-client-library/java/google-api-java-client/android
} catch (IOException e) {
e.printStackTrace();
}
}
If I try to do the same and call sql.execute() or table.execute() I get:
java.lang.IllegalStateException: Calling this from your main thread
can lead to deadlock
So I'm kinda stuck here and I'd like to know how to proceed from somebody who has experience with the google api client, even better if you can help me to get the result on the map! Thank you.
How to display the fusion tables POIs on the map?
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker to Zurich Oerlikon and move the camera
mMap.addMarker(new MarkerOptions().position(mDefaultLatLng).title("Zurich Oerlikon"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
mDefaultLatLng, 13));
// TODO: add fusion tables POIs
}
To see where I'm stuck and help me, clone my github repo firepol / android-google-maps-fusion-tables on github, open it in Android Studio, add your own Google Maps API Key and debug on your device. Thanks for your constructive comments, answers and help. Feel free to push on github as well.
In android networks calls are never made on UI/main thread.
Try using an async task if you just want to see things working or import a networking library like volley/robospice if you are developing a full project.
This commit implements the fusion tables query and shows the resulting POIs on google maps
Explanation: GoogleAccountCredential was wrong and had to be replaced with GoogleCredential. To make this work you need to create a service account, role project > service account actor (generate a private key), download the json file and place it under app/res/raw/service_account_credentials.json (in my commit I refer to this file with this precise name, but feel free to raname it and adapt the code to your needs).
Make sure to enable the Fusion Tables API in the API Manager for your project.
I implemented also a class deriving from the AsyncTask to solve the problem of the main thread.
There is some little refactoring in the commit (Location changed to LatLng) but that's it.
Whoever needs to make an android app and place fusion tables POIs on a google map can clone the github repo and have something to begin with (which was also the initial idea of my question).

Get current remote or game controller

How can I get the current input device in my application in java? I want to know is the remote or the game controller, that is being used.
It is an android application that I want to run on Amazon FireTV. Unlike the Amazon Kindle there is no touchscreen but you can use a remote or a game controller. I would like to know if it is possible to detect what kind of input device the user is currently using.
The code I have until now is a standard Cordova Application code, but when I know how to detect the current input device I would make a plugin to pass the value to the javascript code. That is not the problem.
As mentioned in the comments you should provide steps you have already taken or code you have already written to address this functionality as that will help us tweak the most appropriate answer.
As a general rule, you can look at the official docs to identify controllers on Fire TV.
https://developer.amazon.com/public/solutions/devices/fire-tv/docs/identifying-controllers
Basically, you need to write the identification code in your Cordova plugin as follows:
int hasFlags = InputDevice.SOURCE_GAMEPAD | InputDevice.SOURCE_JOYSTICK;
boolean isGamepad = inputDevice.getSources() & hasFlags == hasFlags;
This will allow you to find out if it's a gamepad. For a Fire TV remote the code you need is:
int hasFlags = InputDevice.SOURCE_DPAD;
bool isRemote = (inputDevice.getSources() & hasFlags == hasFlags)
&& inputDevice.getKeyboardType() == InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC;
The InputDevice class is available on the Android developer site:
http://developer.android.com/reference/android/view/InputDevice.html
So you basically need to import that in your plugin class to ensure the above code works fine.
import android.view.InputDevice;

Is there a way to check if Android device supports openGL ES 3.0?

I need to check dynamically if the used device supports openGL ES 3.0.
How can I do that?
I can't find anything in google or here...
I'm going to expand on this answer. There's the initial part in that answer with the configuration info and getting the reqGlEsVersion. Note that it gets the actual OpenGL version of the device, not the required one declared in the manifest as suggested in some comments.
However, I stumbled over a fairly obvious method in the ConfigurationInfo class called getGlEsVersion. It relies on the reqGlEsVersion system in the ConfigurationInfo class, and returns a String value. With some tiny setup, I made a simple test snippet:
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
System.out.println(Double.parseDouble(configurationInfo.getGlEsVersion()));
System.out.println(configurationInfo.reqGlEsVersion >= 0x30000);
System.err.println(String.format("%X", configurationInfo.reqGlEsVersion));
The device I tested on supports and uses GLES 3.2, and the test app declares GLES3.0 in the manifest
Runnig this prints:
3.2
true
30002 //A note about this one: It doesn't print as 0x30002 because of the printing method. But it is the same as 0x30002
The entire point of those three print statements was to show that either way works. It prints the version supported of the device, and not that declared in the manifest as mentioned in some comments.
So you can use either of these ways to check the version of OpenGL ES on a given device. Personally, I use this:
double version = Double.parseDouble(configurationInfo.getGlEsVersion());
System.out.println("Version: " + version);//Optional obviously, but this is the reason I use the String and parse it to a double
if(version < 3)
throw new RuntimeException("Unsupported GLES version");
but this also works (and is slightly more memory efficient):
int version = configurationInfo.getGlEsVersion();
//format and print here if wanted
if(version < 0x30000)
throw new RuntimeException("Unsupported GLES version");
you could of course show a toast and quit, a dialog, an activity, a fragment, whatever you feel like if the user doesn't meet the requirement. You also should, because if it comes from a third party site, there's a chance requirements were bypassed. I just throw an exception because I declare the usage of GLES3 in the manifest, meaning that throw shouldn't happen in the first place.
As for the manifest tag:
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
This doesn't prevent apps with GLES2 or lower from installing it if they got for an instance a direct install from an APK file or through USB debugging. It's a flag that tells Google Play (or any other stores that exist and check for that) that GLES < (in this case) 3 isn't supported and Google Play would prevent the install. But anyone could install it from a mirror that ignores things like that, so the tag itself isn't a way of preventing GLES2 and lower devices from installing it.
And there's of course the eglCreateContext() method, but that only works with the native system. It does not work with things like LibGDX or anything that uses a separate renderer since it would create a different context than the library would use.
And glGetString(GL_VERSION) would work mostly anywhere, assuming the method is supported by the library/framework. It's natively integrated into GLES2 though, and requires actually creating the OpenGL context first. Using the first methods seem like a better choice on Android. However, this is, of course, up to you.
yes download CPU-Z and every piece of info of your phone is in this app.
its on the play store as: CPU-Z.
I think this code will help you
final ActivityManager activityManager =
(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo =
activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x30000;
this article can help you more
http://www.learnopengles.com/android-lesson-one-getting-started/
This is documented here in the Android SDK documentation:
http://developer.android.com/guide/topics/graphics/opengl.html#version-check
You basically have 3 options:
If your app only works with ES 3.0, you request that version in your manifest:
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
You try to create an ES 3.0 context using eglCreateContext(), and check if it succeeds.
You create an ES 2.0 context, and then check the supported version with glGetString(GL_VERSION).
The link above has sample code for solutions 2 and 3.

How can I have a view controller open only on the first launch

I have built a simple app with Xcode 5, using very basic functions. As my app is going to have a large target audience, I want it to support different languages. I have done the translation part, but what I want is a view controller which displays language selection only the first time the app is opened. I am new to developing apps, so please explain me in detail. Thanks in advance.
Use NSUserDefaults to store data between app launches. You will need something like:
static NSString * const kShowIntroductionKey = #"ShowIntroductionKey";
- (void)showOnFirstLaunch
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL wasIntroductionShowed = [userDefaults boolForKey:kShowIntroductionKey];
if (!wasIntroductionShowed){
// Show your screen here!
[userDefaults setBool:YES forKey:kShowIntroductionKey];
[userDefaults synchronize];
}
}
Also may be it will be better to use native iOS localization mechanism.

Android: custom Facebook integration

I need some advice for this matter...
I used the facebook android sdk to create an integration with facebook from my application...I followed this tutorial:
http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an-android-application/
I would need to implement authentication in one activity and the function postToWall in another.... after authentication i want to send post simply by pressing a button but in other activity, different from that where i do authentication.
is it possible? or with the SDK I'm forced to do everything together in the same activity?
thanks in advance
Yes it is possible. You will get a access token which you can send to the next activity. Use getAccessToken() and setAccessToken().
Here is an example that even saves the needed data: Contact-Picture-Sync
you need to install an extension, similar to the core Android SDK, but no, here is what you need to do:
1.) go to github.com/facebook/facebook-android-sdk
2.) download the facebook directory ONLY! The other directories are only examples.
3.) Put the files from the src (you can copy the drawables too, if you want to) in the package, you are currently working with
4.) You are good to go, you can use the facebook "SDK"
see also this example https://github.com/facebook/facebook-android-sdk/tree/master/examples/Hackbook download it , it is working example provided by facebook
just to provide an alternative answer, there's other ways of implementing sharing on Android.
It allows for more sharing options (like Twitter, QR-Barcodes, blogging and whatnot) without having to deal with the facebook android sdk.
What you would use is a "share" intent, like so:
String title = "My thing"; // used if you share through email or channels that require a headline for the content, always include this or some apps might not parse the content right
String wallPost = "Hey - check out this stuff: http://link.com "; // the content of your wallpost
String shareVia = "Share this stuff via"; // the headline for your chooser, where the phones avaliable sharing mechanisms are offered.
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, wallPost);
startActivity(Intent.createChooser(shareIntent, shareVia));
This is by far the preferred solution on Android if you're looking for simple sharing, as it makes your app future-compatible with new services. And more lean and flexible for the user too, as there's little to no friction from hitting the share button to posting content.
It can also be seen in this blog post: http://android-developers.blogspot.com/2012/02/share-with-intents.html
I hope you can use this for your project.

Categories

Resources