KakimotOnline

April 26, 2009

Google Maps and the MarkerClusterer solution

Filed under: ASP.NET, google, maps — Tags: , , — nandokakimoto @ 2:37 pm

Hi everyone,

today I’ll be writing a small post about a problem that most people who write maps application deal with: add thousands of markers in you map. A known solution to manage your marker is GMarkerManager. However, today I read about the MarkerClusterer solution in GeoGoogleDevelopers Blog.

MarkerClusterer collects markers into different clusters and displays the number of markers in each cluster with a label, creating new clusters as the map zoom level changes. The clustering algorithm is simple; for each new marker it sees, it either puts it inside a pre-existing cluster, or it creates a new cluster if the marker doesn’t lie within the bounds of any current cluster.”

The effect of using MarkerClusterer is shown in the picture below and library’s sample site:

markerclusterer

The library works like magic. You just have to pass your array of markers to the MarkerClusterer constructor, and it ill take care of the rest. First, we have to import the MarkerClusterer JavaScript implementation file. Later, we create some markers, push into the array and call MarkerClusterer, as shown in the code snippet below.

marketclusterercode

It’s really awesome and straightfoward. Try it yourself.

See you,


Fernando

April 6, 2009

C# Extension Methods

Filed under: C#, programming, software design — Tags: , — nandokakimoto @ 1:29 am

I’ve been talking about Object-Oriented Principles for a while, and this post will not be different. Here and here, I’ve introduced OOP and described an elegant solution to comply with Open-Closed Principle using Visitor Pattern, respectively. Today I’ll be talking about a C# 3.0 feature called extension methods. Maybe you’re now asking what extension methods and OOP have in common. That is what this post is about.

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Therefore, extension methods are an optional technique to extend classes functionalities without modifying it. This makes me remember the Open-Closed Principle which states that software entities should be open for extension, but closed for modification. By using extension methods, your software tends to be less difficult to change, to not impact in dependent modules and even more important, changes are achieved by adding new code, not by changing old code that already exists.

Extension methods are defined as static methods in static classes, but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. The following example shows extension methods for C# string class.

StringExtension.cs

em1

Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. They are used as if they were instance methods and are visualized in VS intellisense, as show below:

em2

Here is the test class for StringExtension.cs class showing how to invoke extension methods in C#.

TestExtensionMethods.cs

em3

As you could see, this feature enables you to extend the string class, which is a sealed class, same as final class in Java, and implossible to inherit it. This helps you to extend and maintain code in a really elegant way. So, remeber using extension methods when find similar problems in you code.

See you,


Fernando

Blog at WordPress.com.