1
votes

I'm looking to embed tweets from selected areas in a mapbox map. Is it possible, can rich media appear in a mapbox popup? So far I've managed to get a map to display a link to a selected tweet but not the tweet itself.

I've posted part of the geojson that i'm using to show markers and popups. I placed the javascript for the tweet just after the geojson. Any help would be greatly appreciated, thanks.

<head>
    <meta charset='utf-8' />
    <title>Example</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.39.1/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.css' rel='stylesheet' />
    <style>
      body {
        margin: 0;
        padding: 0;
      }

      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
      }
	  
	  .mapboxgl-popup {
        max-width: 400px;
        font: 12px/20px 'Work Sans', Arial, Helvetica, sans-serif;
    }

    </style>
  </head>
  <body>
    <div id='map'></div>
    <script>
    mapboxgl.accessToken = ; // enter access token
    
	var map = new mapboxgl.Map({
      container: 'map',
      style: , // enter style URL
	  center: [-6.2285,53.3475],
	  zoom: 14
    });
	
	var nav = new mapboxgl.NavigationControl();
    map.addControl(nav, 'top-left');

    // Add geolocate control to the map.
	map.addControl(new mapboxgl.GeolocateControl({
		positionOptions: {
			enableHighAccuracy: true
		},
		trackUserLocation: true
	}));
    
map.on('load', function () {

    map.addLayer({
        "id": "places",
        "type": "symbol",
        "source": {
            "type": "geojson",
            "data": {
                "type": "FeatureCollection",
                "features": [{
                    "type": "Feature",
                    "geometry": {
					    "type": "Point",
                        "coordinates": [-6.2285,53.3475]
                    },
                    "properties": {
                        "title": "3arena",
                        "icon": "stadium",
              //Twitter Timeline
						"description": "<a class='twitter-timeline' data-tweet-limit='1' href='https://twitter.com/search?q=%403arenadublin' data-widget-id='895222749572063232'>Tweets about @3arenadublin</a>"
                    }
                }]
            }
        },
        "layout": {
            "icon-image": "{icon}-15",
			"icon-size": 2,
			"icon-allow-overlap": true
        }
    });
});

//function to embed tweet
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

    // When a click event occurs on a feature in the places layer, open a popup at the
    // location of the feature, with description HTML from its properties.
    map.on('click', 'places', function (e) {
        new mapboxgl.Popup()
            .setLngLat(e.features[0].geometry.coordinates)
            .setHTML('<h3>' + e.features[0].properties.title + '</h3><p>' + e.features[0].properties.description + '</p>')
            .addTo(map);
    });

    // Change the cursor to a pointer when the mouse is over the places layer.
    map.on('mouseenter', 'places', function () {
        map.getCanvas().style.cursor = 'pointer';
    });

    // Change it back to a pointer when it leaves.
    map.on('mouseleave', 'places', function () {
        map.getCanvas().style.cursor = '';
    });


</script>
  </body>
</html>
1

1 Answers

1
votes

The development team of Twitter themselves wrote a complete article about embedded tweets. I really recommend you to check it out, since it propose two ways that might be pretty interesting in your case:

  • Convert Tweet URLs using oEmbed
  • Render a Tweet with JavaScript

oEmbed:

Basically you will just need to add a reference to the oEmbed API in your code, and you will be able to display embedded tweets with a simple link, like so : https://publish.twitter.com/oembed?url=https://twitter.com/Interior/status/463440424141459456 .

Javascript:

Twitter provides developers a Javascript widget which contains a lot of pretty usefull function to decide when and where a tweet should be displayed on your website. This could be a pretty good solution in your case, and I really recommand you to read the article about it from the Twitter team.