Coder Social home page Coder Social logo

leaflet.utfgrid's Introduction

Leaflet.utfgrid

A UTFGrid interaction implementation for Leaflet that is super small.

Example: http://danzel.github.com/Leaflet.utfgrid/example/map.html

Using the plugin

See the included example for the plugin in action.

Usage

Create a new L.UtfGrid, optionally specifying the resolution (The default is 4)

var utfGrid = new L.UtfGrid('http://{s}.tiles.mapbox.com/v3/mapbox.geography-class/{z}/{x}/{y}.grid.json?callback={cb}', {
	resolution: 2
});

?callback={cb} is required when using utfgrids in JSONP mode.

Add event listeners to it

utfGrid.on('click', function (e) {
	//click events are fired with e.data==null if an area with no hit is clicked
	if (e.data) {
		alert('click: ' + e.data.admin);
	} else {
		alert('click: nothing');
	}
});
utfGrid.on('mouseover', function (e) {
	console.log('hover: ' + e.data.admin);
});
utfGrid.on('mousemove', function (e) {
	console.log('move: ' + e.data.admin);
});
utfGrid.on('mouseout', function (e) {
	console.log('unhover: ' + e.data.admin);
});

The callback object in all cases is:

{
	latlng: L.LatLng
	data: Data object for the grid (whatever you are returning in the grid json)
}

We use JSONP by default which requires the query string part of the url to contain callback={cb}. To use an ajax query instead you need to set useJsonP:false in the L.UtfGrid options. Your grid json provider must return raw json to support this functionality.

var utfGrid = new L.UtfGrid('http://myserver/amazingness/{z}/{x}/{y}.grid.json', {
	useJsonP: false
});

Other options

  • pointerCursor: changes the mouse cursor to a pointer when hovering over an interactive part of the grid. (Default: true)
  • maxRequests: Maximum number of requests sent at once to the utfgrid tile server. Increasing this will get more processing done at once, however it means your utfgrid tiles will get priority over your visual tiles (as browsers tend to prioritize javascript/json requests). Increasing this will also reduce the number of requests that may get dropped early when users pan the map. There is little point to have this higher than 8. (Default: 4)
  • requestTimeout: number of milliseconds after which a request for a tile is considered to have timed out. (Default: 60000)

Turning interaction on and off

You can add and remove the UtfGrid layer from your map as per normal, even within a layers control.

Example: http://danzel.github.com/Leaflet.utfgrid/example/layers.html

Other examples of UTFGrid

Spec: https://github.com/mapbox/utfgrid-spec

OpenLayers:

Wax:

leaflet.utfgrid's People

Contributors

aliceh75 avatar bmcbride avatar calvinmetcalf avatar cloudniner avatar danzel avatar gete76 avatar henryptung avatar maximilianloy avatar sephcoster avatar sergioguttal avatar tmcw avatar turban avatar vandelay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaflet.utfgrid's Issues

Uncaught NotFoundError: Failed to execute 'removeChild' on 'Node'

I receive this error ocassionally.

Uncaught NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
  window.(anonymous function).(anonymous function) @ leaflet.utfgrid.js:257
  (anonymous function) @ tile.json?callback=lu0.lu_3_7_1:1

This is the code:

        window[wk][functionName] = function (data) {
            self._cache[key] = data;
            delete window[wk][functionName];
            head.removeChild(script); // <--
            self._finish_request(key);
        };

Max Zoom 18?

Am I correct in understanding the grid only works until zoom level 18?

Add metadata request

Please, add support for metadata request on onAdd or on request. Meta data can contain usefull data. Meta are requested by send a request only with callback, without tile request.

Mouseout only when leaving layer area

Is there a reason why the mouseout functionality in the example is in the mouseover-function?

utfGrid.on('mouseover', function (e) {
if (e.data) {
    document.getElementById('hover').innerHTML = 'hover: ' + e.data.admin;
} else {
    // works like mouseout
    document.getElementById('hover').innerHTML = 'hover: nothing';
}
//console.log('mouseover: ' + e.data);
});

I had a problem with triggering the mouseout too often, while the code in the example didn't work well. Then I changed the mouseout to

if (this._mouseOn && on.data == null) { // the new part '&& on.data == null'
    this.fire('mouseout', { latlng: e.latlng, data: this._mouseOn });
    if (this.options.pointerCursor) {
        this._container.style.cursor = '';
    }
}

and now the mouseout is triggered when leaving the layer or hovering over a region with no values. Could there be any problems with it?

Use a queue for json tile requests

Currently we just add all of the tile requests and expect the browser to download them.
If the user zooms we cannot cancel loading them etc.
We should use a queue instead so this can be handled nicer.

Multiple Grid Layers

How do I deal with multiple active grid layers? Is there any way to trigger grabbing grid data of one grid layer based on the positioning on another grid layer?

Having difficulties figuring out how to approach this.

setUrl method

Is there any way to update the URL after initialization? L.TileLayer has a handy setUrl method for this. It looks like I can use utfGrid.constructor("http://myurl.{z}/{x}/{y}.grid.json?sql=select * from my_table where x = 'y'"), but I have to pan the map a little for it to update. Any thoughts or workaround solutions to handle this? Thanks!

Grid outside of layer boundaries.

I have a base map layer that fits the whole map div (which has no data caked in). I have added a layer that in this case is just 1 state surrounded by nothing but the base map. When you hover outside of the state map an empty data object is being passed and throwing errors for every mouse movement. The problem happens at:

line 145ish of source.
var idx = this._utfDecode(data.grid[gridY].charCodeAt(gridX)),

basically the object passed for "data" is empty I believe the code at 142 needs to be looking for a null data object and an empty.

initialize function issue

Not sure if this is really a bug but more to do with intention. I noticed the new changes now load the Utfgrid with 'this' as the parameter to be passed for the window parameter. Passing 'this' assumes that in the context of loading this file that 'this' is the window object or some other object with global functions. I now get a window.setTimeout is not function because I load Utfgrid into a browserify module and 'this' is definitely not the window object in its context. What's the purpose of passing 'this' and not 'window' in the wrapper function?

Layer is not added after zooming into min/max zoom range

Hi,

I noticed something when working with a large layer that only is displayed on zoom levels 15-18. If the layer is added at a zoom above the minzoom, say 16, and you zoom into the layers range, 15, the layer does not request the necessary grid tiles. The layer has been added, its just not requesting data.

I took a look at the src and it looks like the zoom is compared to min/max zoom in the onAdd event. However, it returns before the move events are applied. Meaning if a layer is added while zoomed out, then you'll never actually be able to fire the moveend event that updates the layer.

Is this check necessary for anything else? Removing that check does not seem to cause any issues and resolves my problem but I want to make sure there are no unintended consequences.

Thanks,
Spencer

License

Is this BSD? I'd be happy to replace Wax's implementation with this if you believe it's been cross-browser tested.

get pixel coordinates for mouse events

so this is more of a question than a real issue. Is it possible to get Screen coordinates for mouse events e.g. mouseover/mousemove? The only thing that I saw that might help me was the latlng Object that contains the map coordinates of the event. Is there any map-coordinates-to-pixel-coordinates conversion available for leaflet?

Regards,
Markus

Between-zoom support

Seems like this plugin will not work at all with leaflet maps with non-integer-locked zooms, no?

utfgrid example mb-util

I am working with mb-util to export the utfgrid and mbtiles with the following command:
mb-util --scheme=xyz /folder/tile.mbtiles /var/www/html/my-tiles
when using the same syntax like in your example I get the following error in the console:
ReferenceError: grid is not defined
my grid.json files normaly look like this:
grid({"keys": ["", "6402"], "data": {"6402": {"namen": "cool name", "attrib 1": 0.123456}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ", " !!!!! ", " !!!!!!! ", " !!!!!!!! ", " !!!!!!!!! ", " !!!! !!!! ", " !! !!!!! ", " !!! !!!!! ", " ! ! ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]});
Is it possible for you to update your example/instruction for mb-util users who would like to be independet from mapbox?!

Precision

Hello,

At zoom levels <= 16, it is quite hard to click on items, even more on mobile. It could be useful to be able to set the radius of the clickable zone.

Regards,

sparse tile sets don't work

If you have a utfgrid tileset where empty tiles are omitted (aka respond with a 404) then this plugin doesn't work because (from what I can tell), errored requests are never removed from the request queue eventually saturating it preventing new requests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.