Skip to main content

Notifications

Customer Service forum

Retrieving location

Posted on by Microsoft Employee

How to detect location (Longitude and latitude) from PC or Laptop  to save this information into CRM record using Plugin or JS code.

Categories:
  • Suggested answer
    Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: Retrieving location

    Hi,

    You can use this sample codes below, I checked Dynamics Client type (web - mobile) and if it's mobile I'm using directly Dynamics CRM client API.

    Please call "getGeoLocation" in your function

    getGeoLocation = function () {
        var clientContext = Xrm.Utility.getGlobalContext().client;
        var client = clientContext.getClient();
    
        if (client == "Mobile") {
            Xrm.Device.getCurrentPosition().then(
                function success(position) {
                    updateLocationOnForm(position.coords.latitude, position.coords.longitude);
                },
                function (error) {
                    geolocationExceptionHandler(error);
                }
            );
        }
        else if (client == "Web") {
            if (navigator.geolocation) {
                var options = {
                    enableHighAccuracy: true
                };
    
                navigator.geolocation.getCurrentPosition(
                    function (position) {
                        updateLocationOnForm(position.coords.latitude, position.coords.longitude);
                    },
                    function (error) {
                        geolocationExceptionHandler(error);
                    },
                    options);
            } else {
                alert("No geo-location feature for this client");
            }
        }
        else {
            alert("No geo-location feature for this client");
        }
    };
    
    
    
    updateLocationOnForm = function (lat, lon) {
        Globals.Crm.setValue("rms_latitude", lat);
        Globals.Crm.setValue("rms_longitude", lon);
        Globals.Crm.saveForm();
    };
    
    geolocationExceptionHandler = function (error) {
        switch (error.code) {
            case error.PERMISSION_DENIED:
                Globals.Crm.AlertDialog("EXCEPTION : \n('PERMISSION_DENIED')", null);
                break;
            case error.POSITION_UNAVAILABLE:
                Globals.Crm.AlertDialog("EXCEPTION : \n('POSITION_UNAVAILABLE')", null);
                break;
            case error.TIMEOUT:
                Globals.Crm.AlertDialog("EXCEPTION : \n('TIMEOUT')", null);
                break;
            case error.UNKNOWN_ERROR:
                Globals.Crm.AlertDialog("EXCEPTION : \n('UNKNOWN_ERROR')", null);
                break;
        }
    };


  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Retrieving location

    Hi partner,

    You could use Html5 Geolocation to detect your location in real time and then add the Js code into the form.

    Here is the demo code of how to get location by Geolocation.

    https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API

    Hope it helps.

    Best Regards

    Leo

  • Datadvancer Profile Picture
    Datadvancer 353 on at
    RE: Retrieving location

    Find Google Map API, using Webapi the retieve the location data and then populate in the crm.

  • Suggested answer
    Mahadeo Matre Profile Picture
    Mahadeo Matre 17,021 on at

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans