Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map Algorithm (Ajax, Tiles, etc)

I'm working on an app that displays a large image just about the same way as Google Maps. As the user drags the map, more images are loaded so that when a new part of the map is visible, the corresponding images are already in place.

By the way, this is a Javascript project.

I'm thinking of representing each tile as a square div with the image loaded as a background image.

My question: how exactly can I calculate what divs are showing, and when the tiles are moved, how do I tell when a new row of divs have become visible?

Thanks!

like image 454
rodrigo-silveira Avatar asked Aug 22 '26 15:08

rodrigo-silveira


1 Answers

About calculating what divs are showing: learn the algorithm for intersecting two rectangles (the stackoverflow question Algorithm to detect intersection of two rectangles? is a good starting point). With that, the divs that are showing are the ones whose intersection with the "view window" is non-empty.

About telling when a new row of divs have become visible: you will probably need a updateInterface() method anyway. Use this method to keep track of the divs showing, and when divs that weren't showing before enter the view window, fire a event handler of sorts.

About implementation: you should probably have the view window be itself a div with overflow: hidden and position: relative. Having a relative position attribute in CSS means that a child with absolute position top 0, left 0 will be at the top-left edge of the container (the view area, in your case).

About efficiency: depending on how fast your "determine which divs are showing" algorithm ends up being, you can try handling the intersection detection only when the user stops dragging, not on the mouse move. You should also preload the areas immediately around your current view window, so that if the user doesn't drag too far away, they will already be loaded.

Some further reference:

  • Tile5: Tiling Interfaces
  • gTile: Javascript tile based game engine
  • Experiments in rendering a Tiled Map in javascript/html…
like image 73
Rafael Almeida Avatar answered Aug 24 '26 04:08

Rafael Almeida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!