Create a tweet heatmap with the Google Maps API

 2014-09-14 4.23.20.jpg

You can create a heatmap using the Google Maps API. This time, based on the location information of the tweet acquired at random, I would like to express "where is the current twitter user" with heatmap.

First get the tweet.

import twitter
stream = TwitterStream(auth=OAuth(Various keys))
iterator = stream.statuses.sample()
locations = []
for tweet in iterator:
    if len(locations) > 99:
        break
    if not 'geo' in tweet:
        continue
    if tweet['geo'] is not None:
        geo = tweet['geo']['coordinates']
        locations.append(geo)

Exhale this location information as csv.

with open('some.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(locations)

This time I wrote csv directly without reading it with JavaScript.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Heatmaps</title>
    <style>
      /* to have the map be displayed as large as possible */
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=visualization"></script>
    <script>
var map, pointarray, heatmap;

// sample data
var sampleData = [
  new google.maps.LatLng(41.869741,-87.721538),
  new google.maps.LatLng(48.19395,-2.125265),
  new google.maps.LatLng(3.580724,98.628154),
  new google.maps.LatLng(4.580501,-74.081261),
  new google.maps.LatLng(-26.882332,26.659449),
  new google.maps.LatLng(-24.654812,25.958468),
  new google.maps.LatLng(29.968636,-95.459855),
  new google.maps.LatLng(48.713171,44.502881),
  new google.maps.LatLng(34.93339,-81.985014),
  new google.maps.LatLng(40.376906,-105.518854),
  new google.maps.LatLng(-2.122002,-79.906566),
  new google.maps.LatLng(40.777568,30.396123),
  new google.maps.LatLng(-29.894143,-50.247492),
  new google.maps.LatLng(40.829574,-96.726031),
  new google.maps.LatLng(44.228229,-76.529446),
  new google.maps.LatLng(-22.791815,-43.363159),
  new google.maps.LatLng(41.012075,28.951118),
  new google.maps.LatLng(43.455311,6.696321),
  new google.maps.LatLng(40.987747,28.878675),
  new google.maps.LatLng(44.511357,-73.062096),
  new google.maps.LatLng(38.581522,-89.925273),
  new google.maps.LatLng(43.301556,5.368706),
  new google.maps.LatLng(38.091917,-3.645525),
  new google.maps.LatLng(36.897981,30.633576),
  new google.maps.LatLng(10.118712,-71.253339),
  new google.maps.LatLng(51.189495,5.528259),
  new google.maps.LatLng(-23.531079,-46.526215),
  new google.maps.LatLng(40.8369,-74.114526),
  new google.maps.LatLng(37.13042,-94.479282),
  new google.maps.LatLng(39.640009,-77.716111),
  new google.maps.LatLng(-13.013756,-38.486381),
  new google.maps.LatLng(-34.201277,-56.342408),
  new google.maps.LatLng(10.919255,-74.80918),
  new google.maps.LatLng(42.69738,-83.245338),
  new google.maps.LatLng(51.453926,-3.180561),
  new google.maps.LatLng(28.658053,77.225473),
  new google.maps.LatLng(43.035312,-76.13877),
  new google.maps.LatLng(26.368914,50.123148),
  new google.maps.LatLng(-31.378565,-64.165087),
  new google.maps.LatLng(35.368463,-81.506347),
  new google.maps.LatLng(33.757625,-84.401357),
  new google.maps.LatLng(40.491088,-3.662337),
  new google.maps.LatLng(33.528219,-85.133143),
  new google.maps.LatLng(41.807787,12.479548),
  new google.maps.LatLng(-37.970056,-57.612154),
  new google.maps.LatLng(51.98949,1.392392),
  new google.maps.LatLng(41.512915,-81.466155),
  new google.maps.LatLng(7.912032,-72.493505),
  new google.maps.LatLng(-23.351162,-51.1873),
  new google.maps.LatLng(36.201708,-81.700396),
  new google.maps.LatLng(43.112279,-83.652649),
  new google.maps.LatLng(51.877076,12.653746),
  new google.maps.LatLng(6.420195,100.416817),
  new google.maps.LatLng(31.325244,37.361949),
  new google.maps.LatLng(48.9481,2.236714),
  new google.maps.LatLng(-27.708866,-53.962288),
  new google.maps.LatLng(42.980997,-87.94887),
  new google.maps.LatLng(40.151149,-104.938639),
  new google.maps.LatLng(32.884038,-97.297599),
  new google.maps.LatLng(58.036561,39.111782),
  new google.maps.LatLng(40.254157,-75.807626),
  new google.maps.LatLng(-34.808521,-56.160179),
  new google.maps.LatLng(41.8369,-87.6844),
  new google.maps.LatLng(10.118712,-71.253339),
  new google.maps.LatLng(30.033776,-89.955507),
  new google.maps.LatLng(38.618279,27.422036),
  new google.maps.LatLng(-22.972435,-43.186386),
  new google.maps.LatLng(-7.647608,111.528889),
  new google.maps.LatLng(14.838083,-91.500155),
  new google.maps.LatLng(43.451903,-83.949347),
  new google.maps.LatLng(52.624694,-1.197507),
  new google.maps.LatLng(36.106227,-5.448284),
  new google.maps.LatLng(34.420831,-119.69819),
  new google.maps.LatLng(39.243791,-82.191701),
  new google.maps.LatLng(43.6177,-73.962748),
  new google.maps.LatLng(38.403184,26.994669),
  new google.maps.LatLng(53.773308,-1.765792),
  new google.maps.LatLng(51.32552,-1.053326),
  new google.maps.LatLng(42.146086,-72.400803),
  new google.maps.LatLng(-34.601558,-58.530852),
  new google.maps.LatLng(52.235012,-7.113571),
  new google.maps.LatLng(46.254375,7.004024),
  new google.maps.LatLng(41.056631,28.865406),
  new google.maps.LatLng(-16.734154,-49.30822),
  new google.maps.LatLng(38.702756,-8.970666),
  new google.maps.LatLng(41.079022,28.924318),
  new google.maps.LatLng(-22.467244,-50.609785),
  new google.maps.LatLng(50.807499,0.294177),
  new google.maps.LatLng(52.296889,4.96612),
  new google.maps.LatLng(-27.684219,-48.483165),
  new google.maps.LatLng(-34.8927,-56.160802),
  new google.maps.LatLng(28.069146,-82.433176),
  new google.maps.LatLng(-23.962148,-46.323084),
  new google.maps.LatLng(32.409653,-90.137747),
  new google.maps.LatLng(-6.1995,106.83294),
  new google.maps.LatLng(33.980423,72.186674),
  new google.maps.LatLng(51.513518,-0.123417),
  new google.maps.LatLng(-27.586342,-48.613531),
  new google.maps.LatLng(24.352714,37.604151),
  new google.maps.LatLng(26.311167,50.222377)
];


function initialize() {
  // the map's options
  var mapOptions = {
    zoom: 2,
    center: new google.maps.LatLng(0, 135),
    mapTypeId: google.maps.MapTypeId.SATELLITE
  };

  // the map and where to place it
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var pointArray = new google.maps.MVCArray(sampleData);

  // what data for the heatmap and how to display it
  heatmap = new google.maps.visualization.HeatmapLayer({
    data: pointArray,
    radius: 10
  });

  // placing the heatmap on the map
  heatmap.setMap(map);
}

// as soon as the document is ready the map is initialized
google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>

  <body>
    <!-- this is where the map finally is drawn onto -->
    <div id="map-canvas"></div>
  </body>
</html>

You can easily create a heatmap like this. You can update it with heatmap.set, so you might be able to pass in real-time location information and see the current tweet user's location at a glance.

References

Recommended Posts

Create a tweet heatmap with the Google Maps API
Create a heatmap with pyqtgraph
Tweet the weather forecast with a bot
Get holidays with the Google Calendar API
Create a REST API to operate dynamodb with the Django REST Framework
Tweet regularly with the Go language Twitter API
Tweet the weather forecast with a bot Part 2
Display Google Maps API with Rails and pin display
Create a translation tool with the Translate Toolkit
Create a clean DB for testing with FastAPI and unittest the API with pytest
How to create a submenu with the [Blender] plugin
Create a Todo app with the Django REST framework
Upload to a shared drive with Google Drive API V3
Create an API with Django
Call the API with python3.
Create a directory with python
Create a new csv with pandas based on the local csv
A note about hitting the Facebook API with the Python SDK
About the camera change event of Google Maps Android API
Probably the easiest way to create a pdf with Python3
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Tornado-Let's create a Web API that easily returns JSON with JSON
Create a web API that can deliver images with Django
Beginners of Google Maps API and Twitter API made "tweet map"
The story of creating a database using the Google Analytics API
Issue reverse geocoding in Japanese with Python Google Maps API
Create a social integration API for smartphone apps with Django
A script that makes it easy to create rich menus with the LINE Messaging API
Get the trading price of virtual currency and create a chart with API of Zaif exchange
[Map display] Display a map from the address registered by the user using the Google Maps JavaScript API and Geocoding API!
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
Create Awaitable with Python / C API
Create an alias for Route53 to CloudFront with the AWS API
[AWS] Create API with API Gateway + Lambda
Specifying the date with the Twitter API
[Python] Hit the Google Translation API
Create a color picker for the color wheel with Python + Qt (PySide)
Create a virtual environment with Python!
Create a model to store information from the Google Books API for intuitive handling and testing
Create REST API that returns the current time with Python3 + Falcon
How to create a heatmap with an arbitrary domain in Python
I tried hitting the Google API with Ruby and Python-Make the database a Spreadsheet and manage it with Google Drive
Hit the Etherpad-lite API with Python
I liked the tweet with python. ..
[LINE Messaging API] Create a BOT that connects with someone with Python
Create API using hug with mod_wsgi
Create a CRUD API using FastAPI
Create a poisson stepper with numpy.random
How to send a request to the DMM (FANZA) API with python
Try to create a Qiita article with REST API [Environmental preparation]
Introducing Google Map API with rails
Create a file uploader with Django
Create a compatibility judgment program with the random module of python.
How to create a serverless machine learning API with AWS Lambda
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 1)
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 2)
Hit a method of a class instance with the Python Bottle Web API
Create a record with attachments in KINTONE using the Python requests module
Create a Python function decorator with Class
Automatically create Python API documentation with Sphinx