SupportMapFragment显示为空白(Map Google V2)

时间:2013-03-25 03:46:17

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

我想在SupportMapFragment容器中加载Fragment。我没有错误,我的API密钥很好,导入了Google Play服务库并调用了所有权限。

我用这种方式调用MapFragment

MapaFragment fragment = new MapaFragment();
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.replace(android.R.id.tabcontent, fragment)          
    .commit();

Fragment如下:

public class MapaFragment extends SupportMapFragment {
private View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    v=inflater.inflate(R.layout.mapa, null);

    return v;       
}

地图的布局就像这样简单:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapa"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

2 个答案:

答案 0 :(得分:1)

你已经在xml布局文件中添加了SupportMapFragment,没有必要在代码中重新添加它或覆盖onCreateView方法,只需注释掉这些

MapaFragment fragment = new MapaFragment();
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.replace(android.R.id.tabcontent, fragment)          
    .commit();

和这些

private View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    v=inflater.inflate(R.layout.mapa, null);

    return v;       
}

代码行并尝试运行应用

答案 1 :(得分:0)

您确定,您的API密钥没问题吗? 请尝试使用此功能检查您的活动的onCreate方法:

 private void getShaKey() {

 try {
 PackageInfo info = getPackageManager().getPackageInfo("YOUR_PACKAGE_NAME",
 PackageManager.GET_SIGNATURES);
 for (Signature signature : info.signatures) {
 MessageDigest md = MessageDigest.getInstance("SHA");
 md.update(signature.toByteArray());
 Log.v(TAG, "KeyHash:" + Base64.encodeToString(md.digest(),
 Base64.DEFAULT));
 }
 } catch (NameNotFoundException e) {
 e.printStackTrace();
 } catch (NoSuchAlgorithmException e) {
 e.printStackTrace();
 }

 }

看看,如果它返回相同的内容,就像你放入清单一样:

 <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR_API_KEY" />

然后请检查,如果getMap()没有像这样返回null:

GoogleMap myMap;

myMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapa)).getMap();

if(myMap != null){
...
}

您可以在“return v”语句之前在onCreateView方法中执行此操作。