我制作了一个地图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"/>
知道为什么它不起作用吗?
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>