I need to display two MapFragments with two different locations in an activity. In my XML file I have two MapFragments called "map" and "map_two", and in java my code in onCreate is :
com.google.android.gms.maps.MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(map);
mapCount = 1;
mapFragment.getMapAsync(this);
com.google.android.gms.maps.MapFragment mapFragmentTwo = (MapFragment) getFragmentManager().findFragmentById(map_two);
mapCount = 2;
mapFragmentTwo.getMapAsync(this);
Then I override onMapReady :
@Override
public void onMapReady(GoogleMap googleMap) {
if (mapCount == 1){
mMap = googleMap;
LatLng vannes = new LatLng(47.66, -2.75);
mMap.addMarker(new MarkerOptions().position(vannes).title("Vannes"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(vannes));
} else if (mapCount == 2){
mMap = googleMap;
LatLng bordeaux = new LatLng(44.833328, -0.56667);
mMap.addMarker(new MarkerOptions().position(bordeaux).title("Bordeaux"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(bordeaux));
}
}
My issue is that the two maps show the same location, whereas I want to show two different locations. Do you see how to make it ?
Thank you,
Alex
The best way to do that, is create 2 different callbacks for each map:
public OnMapReadyCallback onMapReadyCallback1(){
return new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng vannes = new LatLng(47.66, -2.75);
mMap.addMarker(new MarkerOptions().position(vannes).title("Vannes"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(vannes));
}
};
}
public OnMapReadyCallback onMapReadyCallback2(){
return new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng bordeaux = new LatLng(44.833328, -0.56667);
mMap.addMarker(new MarkerOptions().position(bordeaux).title("Bordeaux"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(bordeaux));
}
};
}
After add these 2 methods in your Activity
, you have to set them in your Maps:
com.google.android.gms.maps.MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(map);
mapCount = 1;
mapFragment.getMapAsync(onMapReadyCallback1());
com.google.android.gms.maps.MapFragment mapFragmentTwo = (MapFragment) getFragmentManager().findFragmentById(map_two);
mapCount = 2;
mapFragmentTwo.getMapAsync(onMapReadyCallback2());