android studioMap片段为空

n6lpvg4x  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(351)

我一直在尝试实现一个Map片段到我的主屏幕使用谷歌api,屏幕加载,没有发生错误,但Map是一个空白的灰色屏幕只是显示谷歌标志。显示空灰色Map的图像
我已经在开发人员控制台中反复检查了我的api密钥,甚至创建了一个新项目并使用它。我错过了什么蠢事吗?
我的主屏幕类:

public class homeScreenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF018786")));

    if (savedInstanceState == null) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.mapFragment, new MapsFragment())
                .commit();
    }
}

然后我的碎片类:

public class MapsFragment extends Fragment {

public static final String TAG = "TAG";
private OnMapReadyCallback callback = new OnMapReadyCallback() {

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera.
     * In this case, we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to
     * install it inside the SupportMapFragment. This method will only be triggered once the
     * user has installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG,"onMapReady: Used");
        googleMap.getUiSettings().setAllGesturesEnabled(true);
        LatLng sydney = new LatLng(-34, 151);
        googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
};

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_maps, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SupportMapFragment mapFragment =
            (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
    if (mapFragment != null) {
        mapFragment.getMapAsync(callback);
    }
}

}

t5zmwmid

t5zmwmid1#

更新:我解决了这个问题,我不确定到底是什么原因导致了这个问题,因为google云平台说api已经启用,但是被禁用了,并且用一个新的密钥重新启用了,它工作了。

相关问题