谷歌地图安卓版--地图突然不再显示

11

我正在尝试将Google Maps集成到我正在开发的应用程序中,但迄今为止我做得并不顺利。不过,最终我成功地显示了一个SupportMapFragment地图,并设置了位置和缩放级别。

以下是我代码中的功能部分:

@Override
public void onActivityCreated( Bundle savedInstanceState ) {

    super.onActivityCreated( savedInstanceState );

    Location location = BundleChecker.getExtraOrThrow( KEY_LOCATION, new Bundle[] { savedInstanceState, getArguments() } );
    setLocation( location );

    if ( checkGooglePlayServicesStatus() == ConnectionResult.SUCCESS ) {
        setMapFragment( new SupportMapFragment() );
        getActivity().getSupportFragmentManager().beginTransaction().add( R.id.location_detail_mapFrame, getMapFragment() ).commit();

    }

    populateAddress();
    attachButtonListeners();

    Runnable initMap = new Runnable() {

        @Override
        public void run() {

            if ( checkGooglePlayServicesStatus() == ConnectionResult.SUCCESS ) {
                try {
                    GoogleMap map = getMapFragment().getMap();
                    LatLng latLng = getLocation().getAddress().getLatLng( getActivity() );
                    CameraUpdate update = CameraUpdateFactory.newLatLngZoom( latLng, DEFAULT_MAP_ZOOM );
                    map.animateCamera( update );
                }
                catch (IOException e) {
                    Log.e( TAG, e.getMessage(), e );
                    Toast.makeText( getActivity(), "Unable to find location", Toast.LENGTH_SHORT ).show();
                }
            }

        }
    };

    Handler handler = new Handler();
    handler.postDelayed( initMap, 200 );
}

此外,我编写了一个简单的方便方法来从我的地址模型中获取一个LatLng,您也可以对其进行批评:

/*
 * Convenience method to easily check if there is a valid lat & lng in this address
 */
public boolean hasLatLng() {

    return getLatitude() != null && getLongitude() != null;
}

/*
 * Convenience method for use with Google Maps API
 */
public LatLng getLatLng( Context context ) throws IOException {

    LatLng latLng = null;
    if ( hasLatLng() ) {
        latLng = new LatLng( getLatitude(), getLongitude() );
    }
    else {
        String locationString = getStreet() + ", " + AddressUtil.makeCityStateZipString( this );
        Geocoder geoCoder = new Geocoder( context );
        try {
            List<android.location.Address> matches = geoCoder.getFromLocationName( locationString, 2 );
            if ( matches != null && matches.size() > 0 ) {
                double lat = matches.get( 0 ).getLatitude();
                double lng = matches.get( 0 ).getLongitude();
                latLng = new LatLng( lat, lng );
            }
        }
        catch (IOException e) {
            throw new IOException( e );
        }
    }

    return latLng;
}

我知道这段代码不太理想,需要进行重构。这是我第一次使用Google Maps,所以如果您有任何建议,请随时提出。当我试图将MapFragment作为我的布局XML的标签使用时,遇到了很多问题,所以我现在正在以编程方式创建它。

问题的核心:

我从分段服务器获取了一些虚假的地址数据,这导致Address#getLatLng方法返回null,在调用CameraUpdateFactory.newLatLngZoom时引发异常。在遇到此异常后,我无法再从Google获取地图数据。现在地图片段是空白的,logcat中显示消息:

SupportMapFragment with no map content

05-21 18:11:42.903: I/Google Maps Android API(15747): Failed to contact Google servers. Another attempt will be made when connectivity is established.

05-21 18:11:43.093: E/Google Maps Android API(15747): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).

我已经创建了一个新的API密钥,并在清单中替换了当前的API密钥,但没有任何更改。我对上述代码做的唯一更改是处理了null LatLng,但我已经撤消了这些更改,无济于事,试图将代码恢复到可用状态。

另外,更奇怪的是,我构建了包含在Google Play服务Extra中的示例地图项目,它完美地工作(顺便说一下,有一个单独的API密钥)。

我可能做错了什么?是否忽略了明显的问题?


https://developers.google.com/maps/documentation/android/ - kongkea
你使用的是哪个版本的谷歌地图?另外,你是在使用调试密钥还是发布密钥来加载地图? - MiStr
6
@kongkea,你的链接有什么有用的内容吗?只是发布地图开发人员文档的链接而没有其他说明是无用的。 - dm78
@MiStr 我正在使用最新版本的Google Maps API for Android:v2。 - dm78
你是在测试应用程序还是你的应用程序已经在Google Play上了?尝试创建新的API密钥。 - Sharjeel
@Sharj 我的应用程序不在Google Play上。我在原始帖子中提到我已经创建了一个新的API密钥。我已经多次这样做了,但没有任何改变。 - dm78
3个回答

19

这个问题通常源于引用google-play-service库的问题。 可以查看我写的这篇博客文章,了解如何将Google Maps集成到您的应用程序中,尤其是前三个步骤:

Google Maps API V2

另一个原因可能是您没有正确配置Google API控制台,所以我建议您查看这个指南:

Google Maps API V2 Key

还有可能的原因是在清单文件中的权限方面存在问题。您也可以查看第一份指南获取所需的权限。


5
谢谢。显然,合并冲突导致我在清单文件中丢失了<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>权限声明。 - dm78
4
如果有人仍在寻找答案,请查看此处 - 相同的问题:https://dev59.com/02Mm5IYBdhLWcg3wX-AW#17947755 - Oleksii K.
对我而言有效的方法是将目标SDK更改为最新版本(在撰写本文时为18)+更新Google Play服务库。 - Rotem

2

请使用以下方法:

  1. 更新SDK中的Google Play服务。

  2. 手动卸载设备上的应用并重新启动设备。 我已经尝试过,效果非常好。

此外,请获取新的API密钥以编辑来自https://code.google.com/apis/console/的新SH1代码。

您可以从“窗口-首选项-android-buid”中获取您的SH1代码。


希望你的答案能帮助到其他人。在我的情况下,问题只是一个权限不再存在了。 - dm78

0

让Google地图工作起来真是一场Google恶梦。这对我有用:

-使用Android Manager更新Google Play服务 -使用Sha1密钥库(Window->首选项->Android->Build)和项目包名称创建新的apikey。请在https://code.google.com/apis/console/上执行此操作。

有人更改了包名称,使应用程序不能正常运行!

在需要进行情绪管理疗法之前,请尝试这个方法,感谢Google地图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接