Google 地图上的 Fusion Tables v2 兴趣点:Android 示例?

Fusion Tables v2 POIs on Google Maps: Android example?

我正在寻找有关如何在 google 地图上显示兴趣点(保存在融合表中)的示例。

要在 html 和 javascript 中执行此操作非常简单,请查看我的 javascript map with fusion tables example

Fusion Tables page containing my POIs

我的 goal/question 是如何(在编码方面需要帮助)在 Android 应用程序中实现相同的。我是 android 开发的新手,我已经投入了数小时的基础知识以及检查文档和示例。

检查这个非常好Google Maps example for Android我发现可以开始(我的测试应用程序基于此代码)。

Fusion Tables v2 reference(指向googleapi客户端)

Google API Java client samples on github(最过时的:v1 上的示例)

到目前为止,我实现了显示以我最后已知位置为中心的地图并在其上显示标记。

因为找不到很好的例子,所以我决定发布并分享我的发现,请参阅:firepol / android-google-maps-fusion-tables on github

现在我想显示来自融合表的标记。 我一直在执行请求,我尝试通过 google api 客户端执行。

Google API client example for Android

ActivityFeed feed = listActivities.execute();

这是我的代码(我已经放在 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
    // 
    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();
    }
}

如果我尝试这样做并调用 sql.execute() 或 table.execute() 我得到:

java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock

所以我有点卡在这里了,我想知道如何从对 google api 客户端有经验的人那里着手,如果你能帮助我获得更好结果在地图上!谢谢。

如何在地图上显示融合表兴趣点?

@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
}

要查看我卡在哪里并帮助我,请克隆我的 github 存储库 firepol / android-google-maps-fusion-tables on github,在 Android Studio 中打开它,添加您自己的 Google 地图 API 在您的设备上键入并调试。感谢您的建设性意见、回答和帮助。也可以随意推动 github。

在 android 中,网络调用永远不会在 UI/main 线程上进行。

如果您只是想查看运行情况,请尝试使用异步任务;如果您正在开发一个完整的项目,请尝试使用 volley/robospice 之类的网络库。

This commit implements the fusion tables query and shows the resulting POIs on google maps

解释:GoogleAccountCredential 错误,必须替换为 GoogleCredential。要完成这项工作,您需要 create a service account,角色 项目 > 服务帐户参与者(生成私钥),下载 json 文件并将其放在 app/res/raw/service_account_credentials.json(在我的提交中,我用这个精确的名称引用这个文件,但可以随意命名它并根据您的需要调整代码)。

确保在项目的 API 管理器中启用 Fusion Tables API

我还实现了一个class派生自AsyncTask来解决主线程的问题。

提交中有一些小的重构(位置更改为 LatLng),仅此而已。

任何需要制作 android 应用程序并将融合表 POI 放置在 google 地图上的人都可以克隆 github 存储库并开始使用(这也是最初的我的问题的想法)。