title: Need guidance on a Google Map application that has to show 250 000 polylines
body: <p>I am looking for advice for an application I am developing that uses Google Map.</p>
  
  <p><strong>Summary:</strong>
  A user has a list of criteria for searching a street segment that fulfills the criteria. The street segments will be colored with 3 colors for showing those below average, average and over average. Then the user clicks on the street segment to see an information window showing the properties of that specific segment hiding those not selected until he/she closes the window and other polyline becomes visible again. This looks quite like the Monopoly City Streets game Hasbro made some month ago the difference being I do not use Flash, I can’t use Open Street Map because it doesn’t list street segment (if it does the IDs won’t be the same anyway) and I do not have to show Google sketch building over.</p>
  
  <p><strong>Information:</strong>
  I have a database of street segments with IDs, polyline points and centroid.
  The database has 6,000,000 street segment records in it. To narrow the generated data a bit we focus on city. The largest city we must show has 250,000 street segments. This means 250,000 line segment polyline to show.</p>
  
  <p>Our longest polyline uses 9600 characters which is stored in two 8000 varchar columns in SQL Server 2008.</p>
  
  <p>We need to use the API v3 because it is faster than the API v2 and the application will be ported to iPhone. For now it's an ASP.NET 3.5 with SQl Server 2008 application.
  Performance is a priority.</p>
  
  <p><strong>Problems:</strong>
  Most of the demo projects that do this are made with API v2. So besides tutorial on the Google API v3 reference page I have nothing to compare performance or technology use to achieve my goal.
  There is no available .NET wrapper for the API v3 yet.</p>
  
  <p>Generating a 250,000 line segment polyline creates a heavy file which takes time to transfer and parse. (I have found a <a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/fractalLog5Log4.html" rel="nofollow noreferrer">demo</a> of one polyline of 390,000 points. I think the encoder would be far less efficient with more polylines with less points since there will be less rounding.)
  Since streets segments are shown based on criteria, polylines must be dynamically created and cache can't be used.</p>
  
  <p><strong>Some thoughts:</strong></p>
  
  <p><strong><em>KML/KMZ:</em></strong></p>
  
  <p><em>Pros:</em>
  Since it is a standard we can easily load Bing maps, Yahoo! maps, Google maps, Google Earth, with the same KML file. The data generation would be the same.</p>
  
  <p><em>Cons:</em>
  LineString in KML cannot be encoded polyline like the Google map API can handle. So it would probably be bigger and slower to display. Zipping the file at the size it will take more processing time and require the client side to uncompress the data and I am not quite sure with 250,000 data how an iPhone would handle this and how a server would handle 40 users browsing at the same time.</p>
  
  <p><strong><em>JavaScript file:</em></strong></p>
  
  <p><em>Pros:</em>
  JavaScript file can have <a href="http://code.google.com/apis/maps/documentation/polylinealgorithm.html" rel="nofollow noreferrer">encoded polyline</a> and would significantly reduce the file to transfer.</p>
  
  <p><em>Cons:</em>
  Have to create my own stripped version of API v3 to add overlays, create polyline, etc. It is more complex than just create a KML file and point to the source.</p>
  
  <p><strong><em>GeoRSS:</em></strong>
  This option isn't adapted for my needs I think, but I could be wrong.</p>
  
  <p><strong><em>MapServer:</em></strong>
  I saw some post suggesting using MapServer to generate overlays. Not quite sure for the connection with our database and the performance it would give. Plus it requires a plugin for generating KML. It seems to me that it wouldn't allow me to do better than creating my own KML or JavaScript file. Maintenance would be simpler without.</p>
  
  <p><strong><em>Monopoly City Streets:</em></strong>
  The game is now over, but for those who know what I am talking about Monopoly City Streets was showing at max zoom level only the streets that the centroid was inside the Bounds of the window. Moving the map was sending request to the server for the new streets to show. While I think this was ingenious, I have no idea how to implement something similar. The only thing I thought about was to compare if the long was inside the bound of map area X and same with Y. While this could improve performance significantly at high zoom level, this would give nothing when showing a whole city.</p>
  
  <p><strong><em>Clustering:</em></strong>
  While cluster is awesome for marker, it seems we cannot cluster polylines. I would have liked something like <a href="http://googlegeodevelopers.blogspot.com/2009/04/markerclusterer-solution-to-too-many.html" rel="nofollow noreferrer">MarkerClusterer</a> for polylines and be able to cluster by my 3 polyline colors. This will probably stay as a “would have been freaking awesome but forget it”.</p>
  
  <p><strong><em>Arrow:</em></strong>
  I will have in a future version to show a direction for the polyline and will have to show an arrow at the centroid. Loading an image or marker will only double my data so creating a custom overlay will probably be my only option. I have found that <a href="http://wtp2.appspot.com/ArrowLine.htm" rel="nofollow noreferrer">demo</a> for something similar I would like to achieve. Unfortunately, the demo is very slow, but I only wish to show 1 arrow per polyline and not multiple like the demo. This functionality will depend on the format of data since I don't think KML support custom overlays.</p>
  
  <p><strong><em>Criteria:</em></strong>
  While the application is done with ASP.NET 3.5, the port to the iPhone won't use the web to show the application and be limited in screen size for selecting the criteria. This is why I was more orienting on a service or page generating the file based on criteria passed in parameters. The service would than generate the file I need to display the polylines on the map. I could also create an aspx page that does this. The aspx page is more documented than the service way. There should be a reason.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>Should I create a web service to returns the street segments file or create an aspx page that return the file?</li>
  <li>Should I create a JavaScript file with encoded polyline or a KML with longitude/latitude based on the fact that maximum longitude/latitude polyline have 9600 characters and I have to render maximum 250,000 line segment polyline. Or should I go with a MapServer that generate the overlay?</li>
  <li>Will I be able to display simple arrow on the polyline on the next version.</li>
  <li>In case of KML generation is it faster to create the file with <code>XDocument</code>, XmlDocument, <code>XmlWriter</code> and this manually or just serialize the street segment in the stream?</li>
  </ul>
  
  <p>This is more a brainstorming Stack Overflow question than an actual code problem. Any answer helping narrow the possibilities is as good as someone having all the knowledge to point me out a better choice.</p>