Bing Map Enablement for Address in D365

 

Bing Map Enablement for Address

 

 

1.     Login to D365

 

URL:  https://*****.crm8.dynamics.com/

User Name:  demo@******.onmicrosoft.com

password:  *****@

 

2.     Go to Settings > Administration > System Settings and Enable Bing Maps

 

 





 

Go to Account Main Form>




Enter the correct Address fields on account form
Click on “Click here to view Map” link
it will navigate to Bing Map

 



 

Another approach with HTML Web Resource

1.       https://docs.microsoft.com/en-us/bingmaps/v8-web-control/creating-and-hosting-map-controls/creating-a-basic-map-control

Create a Bing Maps Account and Get a Key

Created key is: 'ArlJ8KQ2GPy3dXJaHDyaGxIl-i_Aj16jPacSiefxLEtynj1XgLo_FbLgZsAfabRC'

2.       Go to Solution>Account > Account Form> Main Form editor.

3.       Create one html web resource with below code.


<html>

<head>

    <title></title>

    <meta charset="utf-8">

    <script type="text/javascript">

        function GetMap(executioncontext) {

            var relatedAccounts = getAccountRelatedChildRecords();

            debugger;

            var map = new Microsoft.Maps.Map('#myMap', {

                credentials: 'ArlJ8KQ2GPy3dXJaHDyaGxIl-i_Aj16jPacSiefxLEtynj1XgLo_FbLgZsAfabRC'

            });

 

            //Request the user's location

            navigator.geolocation.getCurrentPosition(function (position) {

                var latitudeMain = parent.Xrm.Page.getAttribute("new_latitude").getValue();

                var longitudeMain = parent.Xrm.Page.getAttribute("new_longitude").getValue();

 

                var locationMain = new Microsoft.Maps.Location(latitudeMain, longitudeMain);

               

                var pushpinMain = new Microsoft.Maps.Pushpin(locationMain, {

                    draggable: true

                });

                var pushpinclick = Microsoft.Maps.Events.addHandler(pushpinMain, 'click',

                    pinClickHandler);

                var pushpindragend = Microsoft.Maps.Events.addHandler(pushpinMain, 'drag',

                    pinClickHandler);

                // return pushpin;

 

                map.entities.push(pushpinMain);

 

                //Center the map on the user's location.

                map.setView({ center: locationMain, zoom: 15 });

                for (var i = 0; i < relatedAccounts.length; i++) {

                    var accId = relatedAccounts[i].accountid;

                    var latitude = relatedAccounts[i].new_latitude;//parent.Xrm.Page.getAttribute("new_latitude").getValue();

                    var longitude = relatedAccounts[i].new_longitude;//parent.Xrm.Page.getAttribute("new_longitude").getValue();

 

                    var location = new Microsoft.Maps.Location(latitude, longitude);

                    location.accId = accId;

                  

                    var pushpin = new Microsoft.Maps.Pushpin(location, {

                        draggable: false,

                    });

                    ////var pushpinclick = Microsoft.Maps.Events.addHandler(pushpin, 'click',

                    ////    pinClickHandler);

                    var pushpindragend = Microsoft.Maps.Events.addHandler(pushpin, 'drag',

                     pinClickHandler);

                    ////// return pushpin;

 

                    map.entities.push(pushpin);

 

                    //Center the map on the user's location.

                    map.setView({ center: location, zoom: 15 });

                }

            });

 

        }

        function getAccountRelatedChildRecords() {

            debugger;

            var results = null;

 

            var accountId = parent.Xrm.Page.data.entity.getId();//parent.Xrm.Page.getAttribute("parentaccountid").getValue();

            var accountFetchXML = "<fetch version = '1.0' output-format='xml-platform' mapping = 'logical' distinct = 'false'>" +

                "  <entity name='account'>" +

                "    <attribute name='name' />" +

                "    <attribute name='new_longitude' />" +

                "    <attribute name='new_latitude' />" +

                "    <attribute name='accountid' />" +

                "    <order attribute='name' descending='false' />" +

                "    <filter type='and'>" +

                "      <condition attribute='parentaccountid' operator='eq' uiname='purushotham' uitype='account' value='" + accountId + "' />" +

                "    </filter>" +

                "  </entity>" +

                "</fetch>";

 

            accountFetchXML = "?fetchXml=" + encodeURIComponent(accountFetchXML);

            var req = new XMLHttpRequest();

            req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts" + accountFetchXML, false);

            req.setRequestHeader("OData-MaxVersion", "4.0");

            req.setRequestHeader("OData-Version", "4.0");

            req.setRequestHeader("Accept", "application/json");

            req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

            req.onreadystatechange = function () {

                if (this.readyState === 4) {

                    req.onreadystatechange = null;

                    if (this.status === 200) {

                        results = JSON.parse(this.response).value;

                    } else {

                        Xrm.Utility.alertDialog(this.statusText);

                    }

                }

            };

            req.send();

            return results;

        }

        function pinClickHandler(e) {

            parent.Xrm.Page.getAttribute("new_latitude").setValue(e.location.latitude)

            parent.Xrm.Page.getAttribute("new_longitude").setValue(e.location.longitude)

 

            callReverseGeocodeService(e.location)

        }

        // call reverse-geocoding service with current pushpin location:

        function callReverseGeocodeService(pinLocation) {

            // Get pushpin location:

            var searchRequest = 'https://dev.virtualearth.net/REST/v1/Locations/' + pinLocation.latitude + "," + pinLocation.longitude + '?output=json&jsonp=revgeoServiceCallback&key=ArlJ8KQ2GPy3dXJaHDyaGxIl-i_Aj16jPacSiefxLEtynj1XgLo_FbLgZsAfabRC';

            var mapscript = document.createElement('script');

            mapscript.type = 'text/javascript';

            mapscript.src = searchRequest;

            document.getElementById('myMap').appendChild(mapscript);

        }

        // Callback for REST Locations request:

        function revgeoServiceCallback(result) {

 

            if (result &&

                result.resourceSets &&

                result.resourceSets.length > 0 &&

                result.resourceSets[0].resources &&

                result.resourceSets[0].resources.length > 0) {

                // Take only first result:

                var revgeoResult = result.resourceSets[0].resources[0];

                var location = new Microsoft.Maps.Location(revgeoResult.point.coordinates[0], revgeoResult.point.coordinates[1]);

 

                parent.Xrm.Page.getAttribute("address1_city").setValue(revgeoResult.address.adminDistrict2)

                parent.Xrm.Page.getAttribute("address1_stateorprovince").setValue(revgeoResult.address.adminDistrict)

                parent.Xrm.Page.getAttribute("address1_country").setValue(revgeoResult.address.countryRegion)

            }

            else {

            }

        }

 

    </script>

    <script type="text/javascript" src="https://www.bing.com/api/maps/mapcontrol?callback=GetMap" async="" defer=""></script>

    <meta>

    <meta>

    <meta>

    <meta>

    <meta>

</head>

<body dir="LTR" onfocusout="parent.setEmailRange();" lang="en-US" style="overflow-wrap: break-word;">

    <div id="myMap" style="position:relative;width:100vw;height:100vh;"></div>

    <!--<div id='mapDiv' style="position:absolute; width:100%; height:100%;"></div>-->

</body>

</html>





Add Html Web Resource on Account Entity Form











 


 

Here I am using two fields in Account Entity “Latitude and Longitude” for mapping the location in Bing Map

For Below Example An Account having 3 child records.




 





 

Here we have 4 pins
1 pin is for main Record and remaining 3 pin are Account related Records

Main Record Pin can Drag and Child Record Pin’s cannot Drag.

On change of main record pin updating the new_latitude and new_longitude and also Address of
address1_city, address1_stateorprovince and address1_country. Fields.







Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest