Show user current location on TextBox without showing Google Map in JavaScript

nauna
 
on Jan 03, 2021 11:31 PM
784 Views

hello,

using this article i want on click of button event it shows user location in textbox not in map 

Show user’s current location on Google Map using GeoLocation API in website

please advice

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 03, 2021 11:34 PM
on Jan 04, 2021 12:49 AM

Hi nauna,

Refer below code.

HTML

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_Key"></script>
<script type="text/javascript">
    function ViewCoOrdinate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (p) {
                var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
                var geocoder = geocoder = new google.maps.Geocoder();
                geocoder.geocode({ 'latLng': LatLng }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[1]) {
                            document.getElementById("txtLocation").value = results[1].formatted_address;
                        }
                    }  
                });
            });          
        } else {
            alert('Geo Location feature is not supported in this browser.');
        }
  
        return false;
    }
</script>
<input type="text" id="txtLocation" />
<input type="button" id="btnView" value="View" onclintclick="return ViewCoOrdinate()" />