我制作了一个 map FragmentActivity,我想在 MainActivity 中显示它,这样我就可以在 Activity 中包含其他元素。
当我运行该应用程序时, map 会启动,但它不会触发 onMapReady,因此它只是一个基本 map ,我无法在其中移动放置标记或以编程方式移动相机。
代码非常基础,我只是将 map 放在 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"/>
知道为什么它不起作用吗?
MainActivity 代码:
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));
}
MapsActivity 代码:
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里面试试,像这样;
<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>
关于java - MainActivity 中的 Android Map Fragment 不调用 onMapReady,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44069080/