我在FragmentActivity中使用SupportMapFragment在Android上正常运行了Google Map API v2。

但是,当没有网络连接(也没有缓存的信息)时,显然不会显示任何地图。 LogCat给我一条错误消息,以确认这一点(“ Google Maps Android API:无法加载地图。无法连接到Google服务器”),但未抛出该错误消息。

我想通过编程方式检测到这种情况,因为在这种情况下我想转发到其他活动?文档声称,如果无法加载任何地图,则在SupportMapFragment上调用的getMap()会返回null,但事实并非如此。

我已经寻找了将近两天的解决方案,但找不到任何东西。我是否完全忽略了某些东西,还是真的无法找出是否确实装有某些东西?任何帮助是极大的赞赏。

编辑:

这是我当前的代码,执行setContentView(用调试器检查)后,错误消息出现在日志上,但是它仍然说ConnectionResult == SUCESS。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    SupportMapFragment smf = ((SupportMapFragment)
    getSupportFragmentManager().findFragmentById(R.id.map));
    map = smf.getMap();

    if (map != null && GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) ==
                                        ConnectionResult.SUCCESS) {
        // doSomething with the map
    } else {
        Toast.makeText(this, "Google Maps not working", Toast.LENGTH_SHORT).show();
        System.out.println(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this));
        // forward to another activity
    }
}

最佳答案

GooglePlayServicesUtil.isGooglePlayServicesAvailable与设备上可用的GooglePlayServices.APK相关。 API不会向您提供何时加载图块的任何指示。

您可以选择:


通过尝试(hackish,简化)检查是否缓存了任何切片:new File(getExternalCacheDir(), "cache_vts_your.package.name.0").exists()
用自己的代码检查互联网可用性
解析logcat以查找错误


编辑:

注意事项3已弃用,因为出于安全原因,不允许应用程序再读取日志

10-04 23:05
查看更多