2013-05-22 61 views
11

我正在努力将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); 
} 

另外,我写了一个简单方便的方法来从我的地址模型得到一个经纬度,你可以批评,以及:

/* 
* 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地图,因此请随时提供建议,告诉我如何做到这一点。尝试在我的布局XML中使用MapFragment时遇到了很多问题,所以我正在以编程方式创建它。

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

SupportMapFragment with no map content

05-21 18:11:42.903:I /谷歌地图的Android API(15747):无法联系谷歌服务器。连接建立后将进行另一次尝试。

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

我已经创建了一个新的API密钥,并替换了我的清单中的当前一个,没有变化。我对上述代码所作的唯一修改是为了解释一个空的LatLng,并且我已经取消了那些无意中将我的代码恢复到功能状态的更改。

此外,为了让事情变得有点陌生,我构建了包含在Google Play Services Extras中的示例地图项目,并且它完美地工作(具有单独的API密钥,btw)。

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

+0

https://developers.google.com/maps/documentation/android/ – kongkea

+0

您使用的是哪种谷歌地图版本?另外,您是否正在使用调试密钥或释放键来显示地图? – MiStr

+6

@kongkea有什么有用的,你打算与你的链接?发布地图开发者文档的链接并且没有其他说明是无用的。 – dm78

回答

19

此问题通常来自于引用google-play-service库时的问题。 看看这个博客文章中,我写了一篇关于如何将谷歌地图整合应用程序,特别是第3步:

Google Maps API V2

这样做的另一个原因可能是你没有配置谷歌API控制台正确,所以我建议你看看这个指南,以及:

Google Maps API V2 Key

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

+5

谢谢。显然合并冲突导致我从清单中丢失。 – dm78

+0

欢迎你,@DavidMays。 –

+0

是的...... READ_GSERVICES上的拼写错误是我的死亡......谢谢! –

2

使用这样的:

  1. 更新了谷歌在SDK中发挥服务。

  2. 手动从设备上卸载应用程序并重新启动设备。 我已经尝试它和它的打算完全兄弟

还做了一件事得到https://code.google.com/apis/console/

新的API密钥,以编辑的新SH1代码,你可以从窗口 - 偏好的Android让你SH1代码 - 建立

+0

希望你的回答会帮助别人。就我而言,这仅仅是一个不再存在的许可。 – dm78

+0

卸载应用程序并重新安装为我工作。 – toobsco42

0

使谷歌地图工作是一个Googlemare。这对我有用:

- 使用Android管理器更新Google Play服务 - 使用Sha1 keystore(Window-> preferences-> Android-> Build)和项目包名称创建一个全新的apikey。做到这一点:https://code.google.com/apis/console/

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

在你需要愤怒管理治疗之前试试这个,这要归功于谷歌地图。