1

I am building an app using google maps, that will display information/photos about points of interest when a user clicks on a marker. The main activity has two fragments, both are visible (the screen is split 50/50). The first fragment displays a map, the second fragment shows information/photos about whichever marker the user has clicked on. The marker details (latitude, longitude, title) and a text description about the point of interest are held in a custom object PointOfInterest.

The PointOfInterest objects are stored in an ArrayList, and my onMapReady() method calls a method called addAllMarkers() to loop through the ArrayList and update the map with all the markers.

So far, everything works fine, all the markers display, however the titles do not display. Here is the code where I add the markers:

private void addAllMarkers() {

    for(PointOfInterest pointOfInterest : ((MainActivity) getActivity()).getPointsOfInterest().values()) {
        addPointOfInterestMarker(pointOfInterest);
    }
}

public void addPointOfInterestMarker(PointOfInterest pointOfInterest) {

    double latitude = pointOfInterest.getLatitude();
    double longitude = pointOfInterest.getLongitude();
    String placeTitle = pointOfInterest.getPlaceTitle();

    LatLng position = new LatLng(latitude, longitude);

    mMap.addMarker(new MarkerOptions()
            .position(position)
            .title(placeTitle));
}

Even if I replace .title(placeTitle) with .title("Some Text"), I get no title appearing. Here is a screen print showing the markers, but no titles.

Screen print showing the markers, but no titles

What I want is text titles like the following example:

screen print showing titles

The XML for the map fragment is:

<?xml version="1.0" encoding="utf-8"?>
<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_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MapsFragment" />

And here is the onMapReady() method:

   public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        // following code is to update location to current location, provided user has
        // given permission to do so.
        if(mMap != null) {
            Context context = getActivity().getApplicationContext();
            int permission = ContextCompat.checkSelfPermission(context,
                    Manifest.permission.ACCESS_FINE_LOCATION);

            if(permission == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            } else {
                requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                        LOCATION_REQUEST_CODE);
            }
        }

        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        // add all the other markers
        addAllMarkers();

        // display user controls i.e., zoom in and out buttons, my location button etc
        UiSettings mapSettings;
        mapSettings = mMap.getUiSettings();
        mapSettings.setZoomControlsEnabled(true);
        mapSettings.setScrollGesturesEnabled(true);
        mapSettings.setTiltGesturesEnabled(true);
        mapSettings.setRotateGesturesEnabled(true);
        mapSettings.setMyLocationButtonEnabled(true);

        mMap.setPadding(0, 0, 0, 0);

        // zoom in on the centre of Clovelly
        LatLng clovelly = new LatLng(50.998128, -4.399118);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(clovelly)
                .zoom(16)
                .build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        // listener for markers
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                markerClicked(marker);
                return true;
            }
        });
    }

I have run the code on a couple of different emulators and a physical device, but cannot get the titles to display.

EAS
  • 366
  • 2
  • 18
  • 1
    From the documentation for marker title: `A string that's displayed in the info window when the user taps the marker.`. So if you tap on a marker, you'll see the title you're adding. As for your desired behavior, this might help: https://stackoverflow.com/questions/14579426/google-android-maps-api-v2-show-marker-title-always – Daniel Nugent Mar 30 '22 at 22:30

0 Answers0