开发者

Google Map Algorithm (Ajax, Tiles, etc)

开发者 https://www.devze.com 2023-03-31 16:46 出处:网络
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 correspo

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?

开发者_如何学Python

Thanks!


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…


There's no reason to implement this yourself, really, unless it's just a fun project. There are several open source libraries that handle online mapping.

To answer your question, you need to have an orthophoto-type image (an image aligned with the coordinate space) and then a mapping from pixel coordinates (i.e. the screen) to world coordinates. If it's not map images, just arbitrary large images then, again, you need to create a mapping between the pixel coordinates of the source image at various zoom levels and the view-port's coordinates.

If you read Google Map's SDK documentation you will see explanations of these terms. It's also a good idea to explore one of the aforementioned existing libraries, read its documentation and see how it's done.

But, again, if this is real work, don't implement it yourself. There's no reason to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号