【发布时间】:2021-04-15 05:51:58
【问题描述】:
我一直在尝试使用谷歌 API 在我的主屏幕上实现一个地图片段,屏幕加载并且没有发生错误,但地图是一个空白的灰色屏幕,只显示谷歌徽标。 Image showing empty grey 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);
}
}
}
【问题讨论】:
标签: java api android-studio google-maps android-fragments