MainActivity 中的 Android Map Fragment 不调用 onMapReady



我做了一个地图FragmentActivity,我想在MainActivity中显示它,这样我就可以在活动中拥有其他元素。

当我运行应用程序时,地图启动,但它不会触发onMapReady,因此它只是一个基本的地图,我无法在其中移动放置标记或以编程方式移动相机。

代码非常基本,我只是将映射放在 MainActivity XML 中。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:map="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/map"
              android:name="com.google.android.gms.maps.SupportMapFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="4"
              tools:context="com.crisser.beitnaonline.MapsActivity"/>

知道为什么它不起作用吗?

主活动代码:

public class MainActivity extends AppCompatActivity implements 
OnMapReadyCallback
{
private FirebaseDatabase firebaseDatabase;
private DatabaseReference firebaseRef;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (AccessToken.getCurrentAccessToken() == null)
    {
        goLoginScreen();
    }
    firebaseDatabase = FirebaseDatabase.getInstance();
    firebaseRef = firebaseDatabase.getReference().child("event");
    firebaseRef.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            for (DataSnapshot event : dataSnapshot.getChildren())
            {
                Log.i("event", event.toString());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError)
        {
        }
    });

}
private void goLoginScreen()
{
    Intent intent = new Intent(this, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_CLEAR_TASK | 
            Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
public void logout(View view)
{
    FirebaseAuth.getInstance().signOut();
    LoginManager.getInstance().logOut();
    goLoginScreen();
}

@Override
public void onMapReady(GoogleMap googleMap)
{
    Log.e("fired", "fired");
    LatLng sydney = new LatLng(-34, 15);
    googleMap.addMarker(new MarkerOptions().position(sydney).title("test"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

地图活动代码:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback
{
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready 
    to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) 
    getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

/**
 * 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)
{
    mMap = googleMap;
    Log.e("fired", "fired");
    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 15);
    mMap.addMarker(new MarkerOptions().position(sydney).title("test"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

是的,你可以这样做;

请尝试内部框架布局,如下所示;

    <FrameLayout ...>
    <fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.blabla"/>
</FrameLayout>

更改

获取 SupportMapFragment 并在地图准备好使用时收到通知。

 SupportMapFragment mapFragment = (SupportMapFragment) 
getSupportFragmentManager()
        .findFragmentById(R.id.map); 

SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.safety_map));

我期望,它会完美地工作。祝你好运!

您必须通过"OnMapReadyCallback"来实现您的活动。然后,您将能够在MapReady上触发回调方法。

要获得 onMapReady 回调,您需要在活动的 onCreate(( 中使用以下代码。

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

先生,也许您在"build.gradle (Module:app("中缺少此依赖项:

implementation 'com.google.android.gms:play-services-maps:17.0.0'

希望对您有所帮助。

最新更新