Geo location service

Using the IGeoLocationService service, you can filter dynamic content items by their geographic location. The service shows where the items are located on a map.
Using this service, one could get all the locations within a circle, which is represented by point (latitude, longitude) and radius.

PREREQUISITES: To use geolocation, you need the SqlServerSpatial.dll installed on the machine you are running Sitefinity CMS. To obtain this file, from the Microsoft® SQL Server® 2008 R2 SP3 Feature Pack site, download and install the SQLSysClrTypes.msi corresponding to the processor architecture (32- or 64-bit) of your Sitefinity CMS solution.

NOTE: The radius property is measured in kilometers.

The IGeoLocationService provides the following methods:


Definition: GetLocationsInCircle(double latitude, double longitude, double radius, DistanceSorting sorting = DistanceSorting.Asc, ItemFilter itemFilter = null)
Gets the locations of dynamic items within a specified circle. The method returns an IGeoLocation interface, which has all the information that is needed to interact with the geo location.

Use the following code sample:

Definition: GetLocation (Guid itemId, string contentType, string customKey, string providerName)
Gets the geo location of the dynamic type if it has any, otherwise returns null. This method can be used when there is reference to the dynamic item. It returns an IGeoLocation item or null if there is no geo location for item with that id, contentType, customKey, and providerName. The custom key is a parameter, which is used to specify something unique for each item.

Use the following code sample:


Definition: UpdateLocation (Guid id, string contentType, string providerName, string customKey, Guid contentItemId, double latitude, double longitude)
Creates or updates an existing item depending on the parameter id. If you pass an empty GUID to this method, that means, that a new item will be created, but if you pass a non-empty GUID, the geo location that corresponds to that id will be updated in the database.

Use the following code sample:

Filter dynamic content items

There are two ways to filter dynamic content items using the new Geo Location Service. The first one is to filter your dynamic module items, which have an Address field with enabled map and the second one is to filter by geo location points for your custom build types.

In the example below, we have a module with two dynamic content types. The first one represents restaurant objects and the second one – cinemas. First you need to publish the items in the system. This can be done either from the back-end interface or programmatically using the code reference for these types. Then you can use the IGeoLocationManager class to filter the items by distance from a specific point (using the FilterByGeoLocation method) and to sort them by distance (using the SortByDistance method).

Use the following code sample:

If a dynamic content type has more than one Address fields, filtering by a particular Address field can be done by specifying its field name as a propertyName when building the item filter.

NOTE: Filtering dynamic content items does not require registering geo location points (in the Geo Location Service), as this comes out-of-the-box.

If the CustomKey is not specified, then the items will be filtered by either of the fields.

Filter items from custom types

The second option is to use the Geo Location Service directly to register your geo location points for your custom types. After you have registered the points, you can filter and sort your items by different criteria including: the type of the items, their provider and the key (name) of the field containing the geo location data.

Use the following code sample:

Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.

Was this article helpful?