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

时间:2013-05-22 14:16:33

标签: 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 );
}

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

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

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

SupportMapFragment with no map content

  

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密钥,顺便说一句)。

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

3 个答案:

答案 0 :(得分:19)

此问题通常源自引用google-play-service库中的问题。 看一下我写的关于如何在你的应用程序中集成谷歌地图的博客文章,特别是前三个步骤:

Google Maps API V2

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

Google Maps API V2 Key

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

答案 1 :(得分:2)

使用这样的东西:

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

  2. 从设备手动卸载应用程序并重启设备。 我已经尝试了它并且它完美地发展了

  3. 还有一件事是从https://code.google.com/apis/console/

    获取新的api密钥来编辑新的sh1代码

    你可以从window-preference-android-buid

    获取你的sh1代码

答案 2 :(得分:0)

让谷歌地图发挥作用是Googlemare。这对我有用:

- 使用Android Manager更新Google Play服务 - 创建一个新的apikey:Sha1密钥库(Window-&gt; preferences-&gt; Android-&gt; Build)和项目包名称。请执行以下操作:https://code.google.com/apis/console/

有人改变了包名,并且正在挫败应用程序!!

借助谷歌地图,您需要在进行愤怒管理治疗之前尝试此操作。