function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael Hedrick 2Michael Hedrick 2 

latitude longitude trigger

Currently I am captuign a users location via script within a visualforce page.
<script>
    window.onload = function() {
        var startPos;
        var geoSuccess = function(position) {
            startPos = position;
            document.getElementById('{!$Component.inpform.pb.pbs.startLat}').value=startPos.coords.latitude;
            document.getElementById('{!$Component.inpform.pb.pbs.startLon}').value=startPos.coords.longitude;
            //alert('@@@ ' + document.getElementsByClassName('findclass')[0].value);
           document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
           document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;

        };
        navigator.geolocation.getCurrentPosition(geoSuccess);
    };
  </script>

This is fine but makes I cannot reuse the logic.  Would it be a better option to use in in a Apex Class so that the location capturing process can be used on any Object?  If so, how can I make the Class univeral to any object?
Thanks
Best Answer chosen by Michael Hedrick 2
lkatneylkatney
Hi Michael,

Hope you are good!

I have created an LWC component just to do this thing. This component is a reusable component and can be used anywhere. If given the record context through RecordPage on lightning app builder, this can also set geolocation fields on any record.

This is the link: https://www.playg.app/play/lwc-to-set-geolocation-to-any-object

Please have a look at it and let me know if you have any more questions.

Thanks

All Answers

lkatneylkatney
Hi Michael,

Hope you are good!

I have created an LWC component just to do this thing. This component is a reusable component and can be used anywhere. If given the record context through RecordPage on lightning app builder, this can also set geolocation fields on any record.

This is the link: https://www.playg.app/play/lwc-to-set-geolocation-to-any-object

Please have a look at it and let me know if you have any more questions.

Thanks
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Hello lkatney,
That looks very promising. So can I call the LWC from a Visualforce Page Controller?
Thank you for your help.
M
lkatneylkatney
Hi Michael,

Yes, you can use LWC on visualforce page. You can do that by going through this link: https://releasenotes.docs.salesforce.com/en-us/summer19/release-notes/rn_lwc_vf.htm

Thanks

 
Michael Hedrick 2Michael Hedrick 2
Thanks lkatney.  I will give this a try.
M