google maps using three.js and webgl

16,254

Solution 1

Millions of clickable data points can be painted on a google map using webgl.

A data point is represented by an x,y pair for a location on the canvas, an int for size, an off screen color, and an on screen color. These values are stored in separate typed arrays.

Each data point has a unique rgb color to act as a key in a lookup table of data point ids.

Create a texture to store the off screen colors and render it to an off screen buffer. On event, load the off screen buffer and use glReadPixels to retrieve the rgb color of the pixel clicked and then find the id in the lookup table. Points on the on screen buffer, what the user sees, can share a common color.

canvas.addEventListener('click', function(ev) {
  # insert code to get mouse x,y position on canvas
  pixels = new Uint8Array(4);
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  if (colorToId[pixels[0] + " " + pixels[1] + " " + pixels[2]]) {
    # Pixel clicked is a data point on the map
  }
});

Webgl code is lengthy, so only the click event is included.

Here is a live demo and a repo. (angular and coffeescript)

Here is a second example using plain js: webgl-picking-geo-polygons

Here is react-webgl-leaflet

The solution is based on Brendan Kenny's Google Maps + HTML5 + Spatial Data Visualization which explains some of the code in the excerpt above at the 30 min mark, and Displaying WebGL data on Google Maps.

The demo features less than ten data points, but you can just as easily paint over 16 million pickable data points using all combinations of rgb values.

Solution 2

I discovered OpenLayers this past week. Very, very impressive framework. I would strongly suggest taking a look at it. OpenLayers.org is an open source JavaScript web mapping library distinguished from other alternatives, like Leaflet or Google Maps APIs, because of its huge set of components.

I spent the entire weekend creating sample apps by integrating OpenLayers with API's such as MapBox, WebGL etc... After all was said and done, I was extremely impressed with OpenLayers - and I plan to make use of OpenLayers in an upcoming POC/Project.


Here is a link to my test harness. From there you can also download the code for all of the examples, too.

Share:
16,254
Linus
Author by

Linus

Updated on June 08, 2022

Comments

  • Linus
    Linus almost 2 years

    I have thousands of points that need to be plotted on google maps and got a very responsive maps using the example from https://github.com/ubilabs/google-maps-api-threejs-layer . Did anyone have a play at this and wondering if it is possible to have different colored markers and possible marker click events?

    Appreciate any pointers or examples online.

  • Linus
    Linus almost 11 years
    Thanks. cool app. I have several thousand markers that need to be displayed and marker clusterer has been quire useful. but when i saw the video from Brendan youtube.com/watch?v=aZJnI6hxr-c i was really impressed and i have not much experience in webgl, started looking at three.js and hence my post.
  • ddeloy
    ddeloy almost 11 years
    I have done a lot of work with PLAY framework, threejs and WebGL. I am sure that I have something in my past project archives that would meet your specs. In the interim, you might check out RaphaëlJS ( raphaeljs.com )...a small JS library that simplifies working with vector graphics. I just posted this little sample on my site: ddeloy.com/new/raphael/raphael_click_event.html