Preventing Icon Collisions

Preventing Icon Collisions

You can prevent custom icons from colliding (overlapping one another) by using the PreventIconCollisions property of the MapOptions class.

Using the PreventIconCollisions Property

If the PreventIconCollisions property is set to false (the default), MapPoint Web Service renders the map in the traditional fashion—by using an icon to mark the location indicated by each Pushpin object that you specify. For example, following illustration shows nine custom points of interest, which are marked by numbered icons. At this zoom level, many of the icons overlap: icons 3 and 7 are completely concealed by other icons, and icons 1, 5, and 8 partially conceal icons that mark built-in points of interest.

If you set the PreventIconCollisions property to true, MapPoint Web Service marks the actual location of each point of interest by using a small square, which is connected to the icon for each Pushpin object by a line. This ensures that no icons overlap.

The following code example illustrates how to create a MapOptions object and set PreventIconCollisions to true.

[Visual Basic] 
'This example assumes that the service instance 
''renderService' has already been created and that 
'the MapPoint Web Service namespace has been imported. 

'Get a terrain style map and prevent icon collisions.
Dim myViews(0) As ViewByHeightWidth
myViews(0) = New ViewByHeightWidth()
myViews(0).CenterPoint = New LatLong()
myViews(0).CenterPoint.Latitude = 43.65900
myViews(0).CenterPoint.Longitude = -70.25400
myViews(0).Height = 200
myViews(0).Width = 300

Dim mapSpec As New MapSpecification()
mapSpec.DataSourceName = "MapPoint.NA"
mapSpec.Views = myViews
mapSpec.Options = New MapOptions()
mapSpec.Options.Style = MapStyle.Terrain
mapSpec.Options.PreventIconCollisions = True

Dim mapImages() As MapImage
mapImages = renderService.GetMap(mapSpec)

 [C#]
//This example assumes that the service instance 
//'renderService' has already been created and that 
//the MapPoint Web Service namespace has been imported. 

//Get a terrain style map and prevent icon collisions.
ViewByHeightWidth[] myViews = new ViewByHeightWidth[1];
myViews[0] = new ViewByHeightWidth();
myViews[0].CenterPoint = new LatLong();
myViews[0].CenterPoint.Latitude = 43.65900;
myViews[0].CenterPoint.Longitude = -70.25400;
myViews[0].Height = 200;
myViews[0].Width = 300;

MapSpecification mapSpec  = new MapSpecification();
mapSpec.DataSourceName = "MapPoint.NA";
mapSpec.Views = myViews;
mapSpec.Options = new MapOptions();
mapSpec.Options.Style = MapStyle.Terrain;
mapSpec.Options.PreventIconCollisions = true;

MapImage[] mapImages;
mapImages = renderService.GetMap(mapSpec);

The following illustration shows a map that uses the same Pushpin objects and map options as the preceding map, but the PreventIconCollisions property has been set to true.

Note that all icons on the map—not just those that were overlapping—are rendered in the new style.

The PreventIconCollisions property does not prevent pushpin labels from colliding with each other or with icons. All pushpin labels that you provide are rendered on the map in the same way, regardless of how you set the PreventIconCollisions property. The following illustration shows the effect of using pushpin labels when the PreventIconCollisions property is set to true. Notice that some labels overlap other labels and icons.