3
votes

I'm using Mapbox with wordpress.

I see that the popup by default has anchor position which generates css.

I can't manage to center the popup on click : for example, some of the popup are truncated when i open it because the map does'nt center on it.

I tried all the solutions i found here, none of them work. I'm not using Json but wordpress loop to display markers and put content in popups. I find no solutions for anything else than json . So i just want to know if it's possible to entirely disable the position of the popup so that i can put it always on the map corner, whatever the marker I click..

2

2 Answers

2
votes

EDIT

Finally I just changed some css to make the popup stick to the left of the map: I disabled the anchor default position with transform:none, I place it in the corner of the map container with top and left..And then I disabled the arrow around the popup.

.mapboxgl-popup{
    transform:none !important;
     top: 15%;
    left: 10px;
}
.mapboxgl-popup-anchor-top .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-bottom .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-center .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-left .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-right .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-bottom-right .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-bottom-left .mapboxgl-popup-tip,
.mapboxgl-popup-anchor-top-right .mapboxgl-popup-tip, 
.mapboxgl-popup-anchor-top-left .mapboxgl-popup-tip{
    display:none !important;
}
-2
votes

use this example from officail map box example :

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Display a popup</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.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>
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [-96, 37.8],
    zoom: 3
});

var popup = new mapboxgl.Popup({closeOnClick: false})
    .setLngLat([-96, 37.8])
    .setHTML('<h1>Hello World!</h1>')
    .addTo(map);
</script>

</body>
</html>

and you can see the example result on the below link :

https://www.mapbox.com/mapbox-gl-js/example/popup/