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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer Service forum

Retrieving location

(0) ShareShare
ReportReport
Posted on by

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
    Mahadeo Matre Profile Picture
    17,021 on at
  • Datadvancer Profile Picture
    on at

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

  • LeoAlt Profile Picture
    16,341 Moderator on at

    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

  • Suggested answer
    Emre GULCAN Profile Picture
    2,379 on at

    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;
        }
    };


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 > Customer Service

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans