Yesterday I was scheduled to present at Microsoft Reading unfortunately things came up at work and I could not attend. A colleague of mine filled in and gave my presentation. For all those who are interested in this presentation the slides, complete source code and database can be downloaded here:
http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Sample%20code/Silverlight.DynamicTileLayers.zip
This demo shows how to dynamically create map tiles from raw data that is stored in SQL 2008 and display them in both the Silverlight and AJAX map control.
Steps to get this demo to work:
- Restore database into an SQL 2008 spatially enabled database.
- Create new database user.
- Update database connection string in web.config file.
- Specify your Bing Maps Application ID in the MainPage.xaml.cs file.
Why use dynamic tile layers:
- You can display a much larger amount of data on a map without performance issues.
- Only tiles that have been viewed are rendered.
- If data does not change, caching can be added for better performance.

#1 by Debasis on January 11, 2011 - 7:46 am
Hi Ricky,
First of all thanks for the post. My requirement is exactally matches what you did.
But my problem is I am having the DB constrain. I am allowed to only use Oracle 10g.
So can you please let me know how to use using Oracle DB the same highlighting all countries in Bing map Silverlight control.
Thanks in advance..
Debasis
#2 by rbrundritt on January 11, 2011 - 10:40 am
Oracle 10g does have support for spatial objects, unfortunately it does not have a set of .NET tools to allow you to handle the data. An alternative is to create stored procedures to query the data and then return well known text for the shapes. You can then parse the well known text and then use that data to generate the map tiles. This would be the closest thing to what is being done in this article.
#3 by Debasis on January 13, 2011 - 6:55 am
Thanks for your quict reply…
My doubt is how to strore the shapes data in Oracle. Will use spatial object or any other data type (blob, varchar2 etc.) in table.
Thanks,
Debasis
#4 by ritesh on March 19, 2011 - 10:08 am
Hi Ricky
Thanks for Great post. but i want to show the state in color for the california.i have already import shap data in the sql data base through shap2sql tool. but not show in the color.please suggest.
Thanks
#5 by rbrundritt on March 20, 2011 - 11:50 pm
Either set the opacity so that the fill color is transparent, or draw a line rather than a polygon. Alternatively if you don’t have a whole lot of data you can look at using the SQL data connector for Bing Maps.
#6 by Arturo on June 21, 2011 - 11:04 pm
Hey Ricky! This demo is great
I’m using it, but I had a few problems before I got it running.
Basically, the problema was when parsing the Latitude and Longitude from the database.
I have my computer configured in Spanish, and when the numbers were parsed, the parse method ignored the decimal dot.
To fix, I changed in the class SQLExtensions (project TileRenderService) line number 98 to
boundary.Add(new Coordinate(double.Parse(point[0], System.Globalization.CultureInfo.InvariantCulture.NumberFormat), double.Parse(point[1], System.Globalization.CultureInfo.InvariantCulture.NumberFormat)));
Hope this will help someone.
Thanks for the demo!
Arturo.
#7 by rbrundritt on June 22, 2011 - 11:59 am
Thanks for the info. I used to have a bad habit of not using invariant culture. I learnt my lesson witht he Taxi Fare Calculator I built. I’ll try and make future posts support globalization.
#8 by Sven on January 24, 2012 - 5:57 pm
Great demo. Has anybody tried to change this dem so you can use linestrings instead of polygons?
#9 by rbrundritt on January 24, 2012 - 6:11 pm
Yes, It should be easy to get this to work with Linestrings. Also take a look at this tool: http://ajaxmapdataconnector.codeplex.com/
#10 by Sven on January 24, 2012 - 6:22 pm
Thanks, Ricky. But I want to use your demo, Codeplex gives a lot of problems when running in Silverlight 5.0. I tried a lot of things to make it work with Linestrings. I guess I have to create a new stored procedure? I tried to update all boundarys in the sample database to linestrings but that does not work…
#11 by Sven on January 25, 2012 - 2:14 pm
Richard, I send you an email with a request for update your demo for use with Linestrings. Is this possible for you? Of course I will pay…
#12 by prakash on May 9, 2012 - 2:16 pm
hi ricky,
m creating my own bing maps.but m unable to add the layers like street,house…toget my maps work better.plz help me out..that in which way i should get it..these things..
#13 by John on June 19, 2012 - 3:36 pm
For some reason, my ProcessRequest is firing multiple times thus causing my shapes to reload multiple times and slowing my application down. It’s also causing an issue as I am placing random colors into my shapes and they are continuously changing. Any thoughts on why this might be happening?
#14 by rbrundritt on June 19, 2012 - 3:47 pm
It’s processing multiple times as there is likely no caching setup in the demo app. The original demo was based on a situation where the data was constantly changing, as such the tiles needed to constantly be regenerated. Adding a cache header to the response should help out a lot. Take a look at this: http://www.dotnetperls.com/cache-aspnet
As for the colors, using a random color for the shapes will likely cause mismatches as each tile is unaware of the other. It would be best to either base the color on one of the properties of the shape or to add a property to each shape for the color.
#15 by John on June 19, 2012 - 4:11 pm
Yes, I’m basically ordering the zip code shapes by a field associated with the shape. When the field changes, I assign a new random color. I changed the ExtractCountry to include a ref territory and a ref countryColor to assign the color.
#16 by John on June 19, 2012 - 4:14 pm
I do not know if the caching will work. I want the colors/territories to cache but them I’m overlaying dynamic data on top of the map so we can see what territory it is falling under easily. I’ll play around with it thought. Thanks
#17 by John on June 19, 2012 - 6:33 pm
I threw the shapes/colors into a session and I only call my database function if the session is null. This prevents the issue from visually happening at least and as you stated, the tiles aren’t altering colors for the same shape. I had a few instances where they were.
Is it possible to have an AltText pop up when I mouseover each shape?
#18 by pete on April 18, 2013 - 6:35 am
Hi there. Great stuff here. I’m also finding that ProcessRequest() is being called multiple times. Even on first load, it’s being called 4 times for each tile. Any idea what it could be? Thanks!
#19 by rbrundritt on April 18, 2013 - 11:26 am
This is likely due to no caching being set up. The Silverlight control downloads not only the tiles on display but the tiles for the next couple of zoom levels at closer zoom levels for the current map view. This is to make for a smoother transition when zooming. This is how the Silverlight control is designed and unrelated to the dynamic tile service. If you implement caching then a single tile will only be downloaded once. If your data does change frequently a small cache time of a few minutes can usually help.
#20 by MO on July 4, 2012 - 1:59 pm
I want to plot more than 20,000 + pins in Bing Map. I have the database with lat and long value. How can I plot the same using tile server.
Your help in this regard is deeply appreciated.
#21 by rbrundritt on July 5, 2012 - 9:43 am
You can use the drawing tools in .NET to draw an image (i.e. pushpin) on the tiles. Take a look at this article which is similar to what you want to do: http://rbrundritt.wordpress.com/2008/10/25/ve-imagery-service-and-custom-icons/
Also, if you want the data to be interactive you could use server side clustering which would allow you to use real pushpins on the map to display this data. http://www.soulsolutions.com.au/Articles/ClusteringVirtualEarthPart4.aspx
#22 by MO on July 10, 2012 - 2:18 pm
I can plot 20,000+ points in Bing map. I faced a new issue. It is related to sql server. Error message:
“The Polygon input is not valid because the ring does not have enough distinct points. Each ring of a polygon must contain at least three distinct points.”
I used Geography datatype and inserted points in the table.
I faced this issue when I zoomed the bing map after 17 level.
Do you have any solution for this?
#23 by hcalx on July 4, 2012 - 10:18 pm
Hi.
Just found your solution, thanks for sharing it. I’m needing a tile server for bing maps, just need your quick advice :)
What do you think, is your solution good to make tiles for around 200.000 polylines, average 5 vertices per line (lets say its gas pipelines spread evenly across all of US)?
TIA
#24 by rbrundritt on July 5, 2012 - 9:45 am
This solution can be used for that. I have created a client application recently that had 175,000 high resolution polygons (1000+ vertices each). What I would recommend is picking a maximium zoom level for showing this layer. At zoom level 1 you won’t get much detail out of this data, but at zoom level 7 or 8 it may start to make sense to show the data.
#25 by John on July 11, 2012 - 5:16 pm
Encountering a System.OutOfMemoryException error on the line wkt = wkt.Replace(“POLYGON ((“, “”).Replace(“))”, “”); in the WKTToPolygon function. Anyone else encounter this or know why it would happen?
#26 by MO on July 12, 2012 - 8:13 am
Hi Ricky,
I can plot 20,000+ points in Bing map. I faced a new issue. It is related to sql server. Error message:
“The Polygon input is not valid because the ring does not have enough distinct points. Each ring of a polygon must contain at least three distinct points.”
I used Geography datatype and inserted points in the table.
I faced this issue when I zoomed the bing map after 17 level.
Do you have any solution for this?
#27 by rbrundritt on July 12, 2012 - 9:31 am
You can use the MakeValid method on your ploygon data in the database to fix them, or use 4 points. The 4th point needs to be the same as the first point in a ring or polygon. This is a requirement for Geography data in SQL
#28 by Miron on September 20, 2012 - 7:10 pm
Looks like Silverlight reference in Silverlight.DynamicTileLayers need to be updated to target Silverlight version 5.
In addition, if you are using SQL Server Denali you will need to apply this workaround
http://connect.microsoft.com/SQLServer/feedback/details/685654/invalidcastexception-retrieving-sqlgeography-column-in-ado-net-data-reader
so that SqlGeometry is loaded using appropriate version of Microsoft.SqlServer.Types library.
***
This is a personal message. Please do not treat it in any other way, be that business, employment or otherwise.
#29 by rbrundritt on September 24, 2012 - 2:50 pm
This blog post was written before Silverlight 5 and SQL 2012. Instead of using this blog post consider using the AJAX or Silverlight map data connector which has all of this already built in. http://ajaxmapdataconnector.codeplex.com/
#30 by Scott on September 21, 2012 - 3:05 pm
Ricky, I have implemented your design however, the page loads extremely slow. I’m coloring in polygons by zip codes. Probably about 60 sections all together that cover the United States. Any suggestions on speeding this up for subsequent requests? I’ve tried adding cache header but that doesn’t seem to help at all.
#31 by rbrundritt on September 24, 2012 - 2:48 pm
The performance will really depend on how complex your polygon data is. If it is high resolution data you will likely find this slow as that would be a lot of data coming from the database. If using a spatial database consider reducing the resolution of the data based on zoom level. If using SQL 2008/2012 or Azure use the Reduce method to reduce the resolution.
#32 by Sreevalli on September 24, 2012 - 2:42 pm
Hi Ricky,
i need the exactly what you did but not for countries, i need for us zipcodes,
where can we get this boundaries data for zip code???
can you help me in this.
#33 by rbrundritt on September 24, 2012 - 2:46 pm
You will need to purchase this data. I’m not aware of any free source of this data.
#34 by Arun Mohan on December 20, 2012 - 7:08 am
Hi Ricky,
Can we export the map to pdf
#35 by rbrundritt on December 20, 2012 - 12:11 pm
You can export to PDF however this is not available out of the box and would require some development. Exporting Bing Maps to a document would fall under the Bing Maps Print terms of Use: http://www.microsoft.com/maps/product/print-rights.html
#36 by Arun Mohan on December 27, 2012 - 11:04 am
We tried export using Silverlight Export/Print to PDF(http://www.microsoft.com/maps/product/print-rights.html), but we get “WriteableBitmap has protected content. Pixel access is not allowed” error while exporting
#37 by Arun Mohan on December 27, 2012 - 11:10 am
We tried export using Silverlight Export/Print to PDF(http://silverlightpdf.codeplex.com), but we get “WriteableBitmap has protected content. Pixel access is not allowed” error while exporting
#38 by mo on February 18, 2013 - 2:01 pm
Thanks for the great article.
tiles are getting cached in browser. Could you tell me how can we avoid it?
Its bit urgent,