【问题标题】:Google Maps Android -- Map suddenly no longer displayedGoogle Maps Android -- 地图突然不再显示
【发布时间】:2013-05-22 14:16:32
【问题描述】:

我正在努力将 Google 地图集成到我正在开发的应用程序中,到目前为止,我在这方面度过了一段相当不愉快的时光。不管怎样,我终于得到了一个显示地图并设置位置和缩放级别的 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 );
}

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

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 密钥,并在我的清单中替换了当前的,没有任何变化。我对上述代码所做的唯一更改是考虑到一个空 LatLng 并且我已经撤消了这些更改,毫无意义地试图让我的代码恢复到功能状态。

此外,为了让事情有点奇怪,我构建了包含在 Google Play Services Extras 中的示例地图项目,它运行良好(顺便说一句,有一个单独的 API 密钥)。

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

【问题讨论】:

  • 您使用的是什么版本的谷歌地图?另外,你是用调试键还是释放键?
  • @kongkea 您的链接有什么有用的吗?发布地图开发者文档的链接,不说别的也没用。
  • @MiStr 我正在使用适用于 Android 的当前版本的谷歌地图 API:v2。
  • 您是在测试应用程序还是您的应用程序在 Google Play 上?尝试创建新的 API 密钥。

标签: android google-maps google-maps-android-api-2


【解决方案1】:

此问题通常源于引用google-play-service 库时的问题。 看看我写的这篇关于如何在您的应用程序中集成 Google 地图的博文,尤其是前 3 个步骤:

Google Maps API V2

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

Google Maps API V2 Key

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

【讨论】:

  • 谢谢。显然,合并冲突导致我从清单中丢失了 。
  • 如果有人还在寻找答案,请在此处查看 - 同样的问题:stackoverflow.com/a/17947755/1891118
  • 对我来说有效的是将 target-sdk 更改为最新的(编写本文时为 18 个)+ 更新 google play services lib
【解决方案2】:

使用这样的东西:

  1. 更新 SDK 中的 google play 服务。

  2. 从设备中手动卸载应用程序并重新启动设备。 我已经试过了,效果很好,兄弟

另外做一件事从https://code.google.com/apis/console/获取新的api密钥来编辑新的sh1代码

您可以从 window-preference-android-buid 获取您的 sh1 代码

【讨论】:

  • 希望您的回答对其他人有所帮助。就我而言,这只是一个不再存在的权限。
【解决方案3】:

让 Google 地图发挥作用是一项 Googlemare。这对我有用:

-使用 Android 管理器更新 Google Play 服务 - 使用以下内容创建一个新的 apikey:Sha1 密钥库(Window->preferences->Android->Build)和项目包名称。这样做:https://code.google.com/apis/console/

有人更改了包名并破坏了应用程序!!

借助谷歌地图,在您需要愤怒管理疗法之前试试这个。

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    相关资源
    最近更新 更多