Bing maps API integration with dynamics 365 CRM

23 May 2021
Blog Image

In our previous blog, we helped you with the integration of Bing Maps API with Dynamics 365 CRM application and creation of Auto Suggestion functionality on the address field. With this blog, we will guide you on how to calculate the distance between a source and the destination address.

Distance Calculation from Source to Destination Address Field into Dynamics 365 CRM

Step 1: Create a web resource name it as Source Web Resource and place all the above code into it. Our Source Web resource will remain the same and will not change.

Step 2: Create another web resource and name it as Destination Web Resource and place the below code:



    style type=”text/css”>

        body {

            margin: 0;

            padding: 0;

            overflow: hidden;

            font-family: ‘Segoe UI’, Helvetica, Arial, Sans-Serif










Step 3: Almost all the above code is the same as explained earlier with some of the additions. Major additional code comes under selectedSuggestions function where we are getting source address field values into our destination address web resource and concatenating all the field values to form full address.

var concatSourceAddress = sourceAddressLine+’,’+sourceAddressCity+’,’+sourceAddressState+’,’+sourceAddressCountry;

Step 4: The most important step is where we are using Microsoft.Maps.Directions.DirectionsManager API and creating the object of it. Also, we are creating an object of Microsoft.Maps.Directions.Waypoint to provide sourceWaypoint and destinationWaypoint and pass our source address and destination address.

We are getting source address from CRM fields through the execution context while the destination address is from the (current) web resource autosuggestion address field.

Step 5: In the below section of the code, we are finally getting distance from the source to the destination address.

function directionsUpdated(e)


//Get the current route index.

var routeIdx = directionsManager.getRequestOptions().routeIndex;

//Get the distance of the route, rounded to 2 decimal places.

var distance = Math.round(e.routeSummary[routeIdx].distance * 100)/100;

//Get the distance units used to calculate the route.

var units = directionsManager.getRequestOptions().distanceUnit;

var distanceUnits = ”;

if (units ==


distanceUnits = ‘km’




//Must be in miles

distanceUnits = ‘miles’



Here, we are creating a new function name directionsUpdated to get the shortest route index and distance using DirectionsManager.getRequestOptions AP. We can just display this distance using an alert field or store this value into the CRM Distance field by using _formContext global variable.

Provide Bing Maps API key in the highlighted section while keeping the rest of the code the same. It’s already been explained earlier in the blog.

Step 6: Similarly create JavaScript function to pass executioncontext object into our source as well as destination web resource created and place this Jscript function onload of that form.

Similarly, place both the web resources created into any entity form where you want distance functionality as shown above and finally Save and Publish.


In this blog, we went through the integration of Distance Calculation with just a few lines of code. Layering your CRM with Bing Maps will enable you to map your sales, accounts, leads, opportunities based on customer demographics lending you an unparalleled competitive edge in the market.