I want to integrate a map view in one of my views.
I have generated a new map fragment. It comes up in a different view and works like a charm.
I then tried to integrate the code in a normal activity (one with actionbar and so forth). It kinda works - comes up on the screen fine, but onMapReady never gets called in that environment (in other words, I stay stuck in Africa, instead of going to Sydney).
Been on it for hours...
Here is the xml code :
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<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"
tools:context="com.example.jp.artfoundui.MapsActivity" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/monument_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_camera" />
And here is the java code :
public class MonumentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monument);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
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(MapsActivity.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;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
}
Any suggestions ?
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...
An alternate solution is to continue using the main activity (a subclass of AppCompatActivity). It turns out that AppCompatActivity can directly implement a OnMapReadyCallback.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With