Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Getting error while accessing the Microsoft.Maps.Location in JavaScript

Posted on by Microsoft Employee

Hi,

I am looking into to get the distance between locations (using Bing Map) in Javascript but getting the error "Cannot read property 'Location' of undefined" error.

The Microsoft.Maps.Location method causing issue. Can anyone help how to fix this issue?

 

Below is my JavaScript code:  

function distanceCalculation(executionContext) {
var formContext = executionContext.getFormContext();

var hLatitude = "17.38405";
var hLongitude = "78.45636";

var bLatitude = "12.971599";
var bLongitude = "77.594566";

getDistanceBetweenLocations(new Microsoft.Maps.Location(hLatitude, hLongitude), new Microsoft.Maps.Location(bLatitude, bLongitude));

}


// Calculate distance between two locations.
function getDistanceBetweenLocations(Location1, Location2) {
Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () {
var vDistance = Microsoft.Maps.SpatialMath.getDistanceTo(Location1, Location2, Microsoft.Maps.SpatialMath.DistanceUnits.Miles);
alert("Distance in Miles : " + vDistance);
});
}

Thanks,

Naveen

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting error while accessing the Microsoft.Maps.Location in JavaScript

    Hi Andrew,

    Ok, let me try this.

    Thanks,

    Naveen

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Getting error while accessing the Microsoft.Maps.Location in JavaScript

    Naveen,

    You will have to load that file "on the fly". Here are few possible ways - stackoverflow.com/.../ways-to-add-javascript-files-dynamically-in-a-page

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting error while accessing the Microsoft.Maps.Location in JavaScript

    Hi Andrew,

    Thanks for your reply.

    Yes, I am trying to use the Bing Map API to achieve this functionality.

    Actually I am referring to the below sample JS code to achieve this. As per my requirement I am implementing this functionality In JS not in HTML.

    mscrmtechie.blogspot.com/.../distance-between-two-locations-bing-maps.html

    I am not sure how I can reference the the below Bing Map API in JS library (Field Onchange event), so that I am getting this error.

    Could you please help how I can refer this API URL in JS library?

    src='www.bing.com/.../mapcontrol async defer

    Thanks,

    Naveen

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Getting error while accessing the Microsoft.Maps.Location in JavaScript

    Hello,

    As far as I understood you want to use Bing Maps Services for calculation of the distance. Did you include the corresponding modules in order to use them in your code? I assume you have not.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans