web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Calculate distance between 2 addresses Dynamics 365

(0) ShareShare
ReportReport
Posted on by 70

Hi everyone,

Is there a way to calculate the distance between 2 addresses (One static and the other one dynamic) ? In Lead entity, I have address1 (Dynamic with auto generated long and lat) based on user input, and address2 is a static location with long and latitude (Stored in fields when record is created through a workflow), how to calculate the distance between both locations and store the distance in miles in a field (lets say field name is new_distance)? 

I’ve been searching and trying to write (JavaScript) custom codes, workflow to create resource booking and retrieve the traveled miles, without any luck! 

Your help is much appreciated

Thank you

I have the same question (0)
  • Arun Vinoth Profile Picture
    11,615 Moderator on at

    you can use any of the discussed options here - stackoverflow.com/.../7920473

  • Amer91 Profile Picture
    70 on at

    Hi Arun,

    Thank you for your reply, but your answer does not answer my question! Also, just FYI sometimes using google maps in dynamics will mess up bing api functionalities! which in my situation will affect the lat and long auto populate feature!

  • Verified answer
    Amer91 Profile Picture
    70 on at

    After days of searching, I finally found a work around!

    I created an HTML web resource with the code below:

    Static Address named 'company' with lat = 47.64 and long=-122.1297.

    Dynamic Address named 'Customer' using address1 lat and long fields.

    I called address1 (Dynamic Address) lat and long using window.parent.Xrm.Page.getAttribute

    window.parent.Xrm.Page.getAttribute("address1_latitude").getValue(), window.parent.Xrm.Page.getAttribute("address1_longitude").getValue()

    and I entered my Bing Map ID: YourBingMapKey

    And it partially solve the majority of the problem. Partially because who ever create the lead has to get miles from the map (Web Resource) and enter it manually into Distance Field (new_distance), unfortunately, someone at every job has to complain!

    Result:

    2020_2D00_01_2D00_14_5F00_19h37_5F00_20.png

    I used this manual to do this https://docs.microsoft.com/en-us/bingmaps/v8-web-control/map-control-concepts/directions-module-examples/calculate-driving-directions

    NOTE: For some reason solution's Bing Map calling Script didn't work so use the one I used.

    ANOTHER SOLUTION https://docs.microsoft.com/en-us/bingmaps/v8-web-control/map-control-concepts/directions-module-examples/directions-with-options

    ANOTHER SOLUTION https://docs.microsoft.com/en-us/bingmaps/v8-web-control/map-control-concepts/directions-module-examples/directions-module-events

    Please let me know if anyone can find a way to retrieve the distance in Miles and store it in the distance field.

    Also, let me know if anyone needs any help

    Don't forget to delete the space in the bing map calling script

    CODE:

    <html><head>

      <title></title>

      <meta charset="utf-8">

    <script type="text/javascript">

          var map;

          var directionsManager;

          function GetMap()

          {

              map = new Microsoft.Maps.Map('#myMap', {});

              //Load the directions module.

              Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function () {

                  //Create an instance of the directions manager.

                  directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

                  //Create waypoints to route between.

                  var workWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: 'company', location: new Microsoft.Maps.Location(47.64, -122.1297) });

                  directionsManager.addWaypoint(workWaypoint);                

                  var seattleWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: 'Customer', location: new Microsoft.Maps.Location(window.parent.Xrm.Page.getAttribute("address1_latitude").getValue(), window.parent.Xrm.Page.getAttribute("address1_longitude").getValue()) });

                  directionsManager.addWaypoint(seattleWaypoint);

                  //Specify the element in which the itinerary will be rendered.

                  directionsManager.setRenderOptions({ itineraryContainer: '#directionsItinerary' });

                  //Calculate directions.

                  directionsManager.calculateDirections();

              });

          }

      </script>

    <script src="https://www.bing.com/api/maps/mapcontrol?

    DELETE THE SPACE callback=GetMap&amp;key=YourBingMapKey" defer="" type="text/javascript" async=""></script>
    <meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta></head>
    <body style='overflow-wrap: break-word;' onfocusout='parent.setEmailRange();'>
    <div id="myMap" style='position:relative;width:800px;height:600px;'></div>
    <div id="directionsItinerary"></div>

    </body></html>

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2025 Season 2

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans