Osprey
Osprey

Reputation: 1545

Add text to MapBox marker

I am currently adding multiple markers with differing icons to a MapBox map using code like below:

L.mapbox.accessToken = 'xxxx';
var map = L.mapbox.map('map')
.setView([XX.0309243, XX.2741612], 14)
.addLayer(L.mapbox.styleLayer('mapbox://styles/mapbox/streets-v11'));
var myLayer = L.mapbox.featureLayer().addTo(map);
var geoJson = {
type: 'FeatureCollection',
features: [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [XX.2741612, XX.0309243]
},
'properties': {
'title': '11-Jun 21:54:56',
'icon': {
'iconUrl': '/images/markers/marker_045.png',
'iconSize': [32, 32],
'iconAnchor': [25, 25],
'popupAnchor': [0, -25],
'className': 'dot'
}
}
}
]
};
// Set a custom icon on each marker based on feature properties.
myLayer.on('layeradd', function(e) {
var marker = e.layer,
feature = marker.feature;
marker.setIcon(L.icon(feature.properties.icon));
});
// Add features to the map.
myLayer.setGeoJSON(geoJson);

Is there anything I can change to this code that would add a label near each marker. Right now, to see the text you need to click on the marker.

Upvotes: 2

Views: 16538

Answers (2)

user10797372
user10797372

Reputation:

I think that you have to create a popup instance before the marker and add it to the map beforehand. Afterwards you can attach it to the marker

var popup = new mapboxgl.Popup()
  .setText('Description')
  .addTo(map);

let marker = new mapboxgl
  .Marker()
    .setLngLat([-96, 37.8])
    .addTo(map)
    .setPopup(popup);

I created a fiddle for you. You can create a method out of the snippet and call the routine everytime you want to add a popup and marker. In your specific case you may have to iterate you cant add the layer directly but have to build some logic in between.

Upvotes: 9

Harry Andriyan
Harry Andriyan

Reputation: 71

Well, that sounds tricky, I think you can try this to solve your problem it's called text-variable-anchor, it allows high priority labels to shift position to stay on the map.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Variable label placement</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.0/mapbox-gl.css" rel="stylesheet" />
<style>
    body { margin: 0; padding: 0; }
    #map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>

<script>
    // TO MAKE THE MAP APPEAR YOU MUST
    // ADD YOUR ACCESS TOKEN FROM
    // https://account.mapbox.com
    mapboxgl.accessToken = '<your access token here>';
    var places = {
        'type': 'FeatureCollection',
        'features': [
            {
                'type': 'Feature',
                'properties': {
                    'description': "Ford's Theater",
                    'icon': 'theatre'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.038659, 38.931567]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'description': 'The Gaslight',
                    'icon': 'theatre'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.003168, 38.894651]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'description': "Horrible Harry's",
                    'icon': 'bar'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.090372, 38.881189]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'description': 'Bike Party',
                    'icon': 'bicycle'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.052477, 38.943951]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'description': 'Rockabilly Rockstars',
                    'icon': 'music'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.031706, 38.914581]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'description': 'District Drum Tribe',
                    'icon': 'music'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.020945, 38.878241]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'description': 'Motown Memories',
                    'icon': 'music'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [-77.007481, 38.876516]
                }
            }
        ]
    };

    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-77.04, 38.907],
        zoom: 11.15
    });

    map.on('load', function() {
        // Add a GeoJSON source containing place coordinates and information.
        map.addSource('places', {
            'type': 'geojson',
            'data': places
        });

        map.addLayer({
            'id': 'poi-labels',
            'type': 'symbol',
            'source': 'places',
            'layout': {
                'text-field': ['get', 'description'],
                'text-variable-anchor': ['top', 'bottom', 'left', 'right'],
                'text-radial-offset': 0.5,
                'text-justify': 'auto',
                'icon-image': ['concat', ['get', 'icon'], '-15']
            }
        });

        map.rotateTo(180, { duration: 10000 });
    });
</script>

</body>
</html>

see more here: https://docs.mapbox.com/mapbox-gl-js/example/variable-label-placement/

Upvotes: 3

Related Questions