Dev: SM: Using Callbacks
Callbacks are a powerful method to have your data and users interact with the map. A quick reference is available here Streetmaps Reference
Example - Large Data Sets
A simple example is shown at callback_test.html - feel free to take the source code and adapt it to your needs.
The Main Page
You can view the source of the test page to see how it works, but the important points are:
- Set the callback with
myMap.RedrawCallback = cb_redraw;; each time the map is redrawn, it will allow you to run code to update the points.
- The callback function gets the viewport bounds with
- The URL is invoked for a JSON call
- The JSON callback at
CallbackTest(data)is executed when the JSON returns
- Current points are removed with
myMap.RemoveAllPoints();to ensure there are as few off-screen items for the API to maintain as possible
- The points returned in the JSON are added with
- The map view is updated with
myMap.Update(false);- it's very important that the false parameter is set here, as it instructs the API to not invoke the RedrawCallback again, getting into an infinite loop.
The Callback Page
See the source code here callback_test_cb.aspx. This is a simple C# page that generates some random points within the current viewport, and is self-explanatory.
- Try and only display more important data the further out the map is zoomed. Displaying too many points on a map is usually useless to the user. A small number of pertinent points makes for a far better user experience, both in browser performance, and the ability to digest the information.