且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android studio onMapReady 未调用

更新时间:2022-05-05 23:11:00

我得到了答案 - 有点.

I got the answer - sort of.

关于片段活动的整个代码永远不会被调用.我尝试了几种方案来激活这个片段,但都无疾而终...

The whole code concerning the fragment activity never gets called. I tried several solutions to activate this fragment, but all came to a dead end...

另一种解决方案是继续使用主活动(AppCompatActivity 的子类).原来AppCompatActivity可以直接实现一个OnMapReadyCallback.

An alternate solution is to continue using the main activity (a subclass of AppCompatActivity). It turns out that AppCompatActivity can directly implement a OnMapReadyCallback.

所以我加了实现OnMapReadyCallback",然后直接加了一个onMapReady"回调.代码如下所示:

So I added "implement OnMapReadyCallback", and then added directly a "onMapReady" callback. Code looks like this :

public class MonumentActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_monument);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.monument_map);
    mapFragment.getMapAsync(this);
[...]
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng louvres = new LatLng(48.860294, 2.338629);
    mMap.addMarker(new MarkerOptions().position(louvres).title("Marker sur le Louvres"));
    // mMap.moveCamera(CameraUpdateFactory.newLatLng(louvres));
    // mMap.moveCamera(CameraUpdateFactory.zoomTo(18));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(louvres, 15));
}

工作正常.我得到了一个完整的活动(工具栏、菜单处于活动状态,还有一个额外的浮动按钮),并且底层地图工作正常......

Works fine. I get a full activity (with the toolbar, menus active, and an additional floating button), and the underlying map works fine...