谷歌地图Android - 地图突然不再显示

dm7*_*m78 11 android google-maps google-maps-android-api-2

我正在努力将谷歌地图集成到我正在处理的应用程序中,到目前为止我还有一段相当不愉快的时间.无论如何,我终于得到了一个显示地图的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 );
}
Run Code Online (Sandbox Code Playgroud)

另外,我写了一个简单的方便方法来从我的地址模型中获取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;
}
Run Code Online (Sandbox Code Playgroud)

我知道这段代码并不理想,需要重构.这是我第一次使用谷歌地图,所以请随时提供关于我如何做到这一点的建议.尝试在我的布局XML中使用MapFragment时遇到了很多问题,所以我正在以编程方式创建它.

问题的核心:我从登台服务器获取了一些伪造的地址数据,这导致Address#getLatLng方法返回null,这在调用CameraUpdateFactory.newLatLngZoom时导致异常.在我遇到此例外后,我无法再从Google获取地图数据.地图片段现在是空白的,消息显示在logcat中:

SupportMapFragment没有地图内容

05-21 18:11:42.903:I/Google Maps Android API(15747):无法联系Google服务器.建立连接时将进行另一次尝试.

05-21 18:11:43.093:E/Google Maps Android API(15747):无法加载地图.联系Google服务器时出错 这可能是一个身份验证问题(但可能是由于网络错误).

我创建了一个新的api密钥,并在我的清单中替换了当前的密钥而没有任何变化.我对上述代码所做的唯一更改是考虑了一个null LatLng,并且我已经在无意义的尝试中撤消了这些更改,以使我的代码恢复到功能状态.

另外,为了让事情变得有点奇怪,我构建了Google Play Services Extras附带的示例地图项目,它完美运行(有一个单独的API密钥,顺便说一句).

我在这里做错了什么?我忽略了一些明显的东西吗

Emi*_*Adz 19

此问题通常源自引用google-play-service库中的问题.看看我写的关于如何在您的应用程序中集成G​​oogle Maps的博客文章,特别是前3个步骤:

Google Maps API V2

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

Google Maps API V2键

可能导致此问题的另一个原因是,如果您在清单文件中的权限中存在某种问题.您还可以查看所需权限的第一个指南.

  • 谢谢.显然合并冲突导致我从我的清单中丢失<uses-permission android:name ="com.google.android.providers.gsf.permission.READ_GSERVICES"/>. (5认同)
  • 如果有人仍在寻找答案,请点击此处 - 同样的问题:http://stackoverflow.com/a/17947755/1891118 (4认同)