SupportMapFragment appearing blank (Map Google V2)

心不动则不痛 提交于 2019-12-12 08:14:46

问题


I want to load a SupportMapFragment in a Fragment container. I get no errors, my API Key is fine, Google Play Services Library is imported and all permissions are called.

I call the MapFragment this way:

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

The Fragment is as follows:

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;       
}

And the layout for the map is as simple as this:

<?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"/>

回答1:


You have already added SupportMapFragment in xml layout file, it's not necessary to add it again in code or override onCreateView method, just comment out these

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

and these

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;       
}

lines of code and try to run the app




回答2:


Are you sure, your API key is fine? Please try this function to check in your activity's onCreate method:

 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();
 }

 }

And see, if it returns the same, as which you put into your manifest:

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

Then please check, if getMap() is not returning null like this:

GoogleMap myMap;

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

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

You can do this in your onCreateView method before the "return v" statement.



来源:https://stackoverflow.com/questions/15607295/supportmapfragment-appearing-blank-map-google-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!