Friday, April 28, 2006

Speeding Up GPolyline Loads

The GPolyline object from Google Maps API is very nice. You feed it an array of GPoints and it creates a line connecting those points. It creates an image overlay of the line on a Google server and sends it back to your map. Or if you're using IE, it will use the VML in IE to draw the line. You can tell it what color, width and opacity to set for the line.

I loaded the shapefile for the Superior trails and found that there were 475 lines with 5,772 data points. This was a pretty incredible load on the application. It takes a long time for a browser to load that much data on a page. And Google has to generate all those line images and send them back for you. So, I was trying to figure out a way to speed up the loading of the polylines. I read from some other sources that cutting down on the number of data points from the shapefile would help. But, I felt that with the hiking trails, I didn't want to lose any detail by leaving out data. Then I wondered if I couldn't use asynchronous requests (AJAX). If I could make each request for a line asynchronously, then it wouldn't seem like the application had died when you added the trails to the map. It worked really great. I built a little PHP script to be called asynchronously for each line (475 times). It would then query the database for the points for that line. It still takes a while to process that many requests, but the map does not block and wait while it's off fetching the lines.

As before, you can see it here: http://gregz.is-a-geek.net

No comments: