Tuesday, January 19, 2010

Google map API + Encoded polyline

Recently I downloaded Subgurim's google map interfacing library for asp.net. It's cool library supporting many origin javascript functions of Google API. And the key point is that you can write a code directly inside server code(well mine is C#) , and you don't need to care server-client side integration. My aim was to display very long and complex trajectories in map. First I drew lines with Subgurim's AddPolyLine function, but it's quite slow , while zooming it takes 3-4 seconds to redraw lines. So by surfing net I found Encoded polylines which is very optimal way to display polylines. Encoded polyline is like a hashed key which stores lines with their different zoom level. Unfortunately Subgurim's library did not include encoded polyline functionality. So I added some manual code:

private void AddEncodedPolyline(string polyLineID, List tracks, string color, string weight)
{
PolylineEncoder enc = new PolylineEncoder();
EncodedPolyline resPol = enc.Encode(tracks);
string A1 = resPol.Points;
A1 = A1.Replace("{", "{{");
A1 = A1.Replace("}", "}}");
A1 = A1.Replace("\\", "\\\\");
string B1 = resPol.Levels;
string zoomFact = resPol.ZoomFactor.ToString();
string numLevels = resPol.NumLevels.ToString();
string js = string.Format(@"var "+polyLineID+" = new GPolyline.fromEncoded({{ color: '" + color + "', weight: " + weight + ",opacity: 1, points: '" + A1 + "', levels: '" + B1 + "', zoomFactor:" + zoomFact + ", numLevels: " + numLevels + "}});{0}.addOverlay(" + polyLineID + ");", gmpa.GMap_Id);
gmpa.addCustomInsideJavascript(js);
}

gmpa is a subgurim's object for interacting with google map api. I used PolylineEncoder class from Gabriel Svennerberg. he translated original polylineEncoder written in java into C#.
Also I used vertexWelder class for welding vertexes. Welding gave 15 times optimization of lines, though the quality of trajectory was same. It removed close located vertexes from trajectory.

No comments:

Post a Comment