Reputation: 566
I'm trying to get values (latitude, longitude) using Geolocation. I tried using the same Javascript (i'll be posting it under the question) in another page, but this Javascript fires only when a button is clicked,
Focus on the OnClientClick="call(); return false;"
<asp:Button ID="BTN_Register" runat="server" Text="Register" OnClientClick="call();return false;" CssClass="btn btn-theme-bg btn-3d btn-xs" OnClick="BTN_Register_Click" />
So returning to my problem.. I am using the same Javascript codes as i was in another WebForm. And the process of it is retrieving the Latitude and Longitude then parsing it into 2 asp:HiddenFields and then saving them into the database using the Code Behind (aspx.cs),
resultRecord = al.createLogEntry(username, externalIP, Convert.ToDouble(latitudeTB.Value), Convert.ToDouble(longitudeTB.Value), "Pending");
Write now in a different WebForm on the HTML side,
These are my codes,
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<p class="text-success" runat="server" id="loginDetails"></p>
<p class="text-danger" runat="server" id="logoutDetails"></p>
<div>
<asp:HiddenField runat="server" ID="latitudeTB"/>
<asp:HiddenField runat="server" ID="longitudeTB"/>
<script type="text/javascript">
//GeoLocation Retrieval
var geo = document.getElementById("geolocationValue");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = lat;
document.getElementById("<%=longitudeTB.ClientID %>").value = lng;
});
} else {
geo.innerHTML = "Geolocation is not supported by this browser.";
}
}
function call() {
getLocation();
}
/*document.onreadystatechange = function (e) {
if (document.readyState === 'complete') {
//dom is ready, window.onload fires later
call();
}
};*/
/*$(window).load(function () {
call();
});*/
</script>
</div>
And in Page_Load i used this RegisterClientScriptBlock to actually cause the HiddenFields to be filled with the Latitude and Longitude respectively to then be saved into the database with the same method from the other WebForm i posted above,
ScriptManager.RegisterClientScriptBlock(this, GetType(), "StartGeo", "call();", true);
Debug.WriteLine(latitudeTB.Value);
Debug.WriteLine(longitudeTB.Value);
I want my Longitude and Latitude values to be present the moment the page loads, But seems to no avail.. I checked the values of the HiddenFields but i'm getting nothing at all.. It means that the HiddenFields are either empty or null?
May i know what can i do to solve this?
Appreciate any help please.. Thank you!
Upvotes: 1
Views: 162
Reputation: 126
You have put wrong variables. Change "latitude" & "longitude" to lat & lng. Check edited code below.
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = lat;
document.getElementById("<%=longitudeTB.ClientID %>").value = lng;
your previous code were.
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = latitude;
document.getElementById("<%=longitudeTB.ClientID %>").value = longitude;
Upvotes: 1