0

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.

firepol
  • 1,731
  • 22
  • 39

2 Answers2

0

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.

Amit Kaushik
  • 642
  • 7
  • 13
  • Thx. Got an updated tutorial or code sample to use Async Task with google api client?I'll look also Robospice etc. found actually a good intro to compare them: https://www.reddit.com/r/androiddev/comments/3d1iim/retrofit_volley_or_robospice_and_jackson_or_gson/ – firepol Jan 28 '17 at 22:56
0

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).

firepol
  • 1,731
  • 22
  • 39