如何检测pogrammatically一个谷歌地图是否被实际装载在Android上的API V2(例如,当网络不可用)?不可用、实际、地图、网络

2023-09-06 10:43:12 作者:薄年欲断 YEAR

我有谷歌地图API V2使用了FragmentActivity一个SupportMapFragment在Android上正常工作。

然而,当没有网络连接(没有缓存的信息),很明显,可以显示没有地图。 LogCat中给我一个错误消息,确认这个(谷歌地图Android版API:无法加载地图无法连接到谷歌服务器),但这不是抛出

我如何编程方式检测这种情况下,因为我想转发给在这种情况下,不同的活动?该文档称,如果没有地图可以加载的GetMap()呼吁SupportMapFragment将返回null,但是这似乎并不如此。

我一直在寻找周围近两天的解决方案,但没能发现任何东西。上午我完全忽视的东西或者是不是真的不可能找到了一些东西是否actualy装?任何帮助是极大的AP preciated。

编辑:

下面是我目前的code,则执行后的setContentView(含调试检查)上的日志出现错误消息,但它仍然表示,ConnectionResult == SUCESS。

 保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    SupportMapFragment SMF =((SupportMapFragment)    。getSupportFragmentManager()findFragmentById(R.id.map));    地图= smf.getMap();    如果(MAP = NULL&放大器;!&安培; GooglePlayServicesUtil.isGooglePlayServicesAvailable(本)==                                        ConnectionResult.SUCCESS){        // DoSomething的与地图    }其他{        Toast.makeText(这一点,谷歌地图不工作,Toast.LENGTH_SHORT).show();        的System.out.println(GooglePlayServicesUtil.isGooglePlayServicesAvailable(本));        //着另一个活动    }} 

解决方案

GooglePlayServicesUtil.isGooglePlayServicesAvailable涉及到GooglePlayServices.APK您的设备上。 API不会给你加载切片时的迹象。

诺基亚HERE地图能否战胜苹果和谷歌地图

您必须的选项:

检查是否有瓦片被尝试(hackish的,简化的)缓存:新建文件(getExternalCacheDir(),cache_vts_your.package.name.0)存在() 检查网络的可用性与您自己的code 解析的logcat发现错误

编辑:

请注意3点是德$ P $由于安全原因pcated因为应用程序不允许再阅读日志

I have the Google Map API v2 working fine on Android using a SupportMapFragment in a FragmentActivity.

However, when there is no network connection (and no cached information), obviously, no map can be shown. LogCat gives me an error message confirming this ("Google Maps Android API: Failed to load map. Could not connect to Google servers"), but this is not thrown.

How can I detect this case programmatically, because I would like to forward to a different Activity in this case? The docs claim that if no map could be loaded getMap() called on the SupportMapFragment would return null, but this does not seem to be the case.

I've been searching around for almost two days for a solution, but was not able to find anything. Am I totally overlooking something or is it really not possible to find out whether something was actualy loaded? Any help is greatly appreciated.

Edit:

Here is my current code, the error message appears on the logs after the setContentView is executed (checked with debugger), but it still says that 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 is related to GooglePlayServices.APK available on your device. API will not give you any indication of when tiles are loaded.

You have the options to:

check if any tiles are cached by trying (hackish, simplified): new File(getExternalCacheDir(), "cache_vts_your.package.name.0").exists() check internet availability with your own code parse logcat to find the error

Edit:

Note point 3 is deprecated because applications are not allowed to read logs anymore due to security reasons