Attempting to retrieve user location using geolocation in JavaScript, I am able to see the current location. However, when trying to assign the coordinates variable to a C# object label, it does not return anything. Below is my JavaScript code snippet:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=ABC"></script>
<script type="text/javascript">
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (p) {
var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
var mapOptions = {
center: LatLng,
zoom: 20,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var marker = new google.maps.Marker({
position: LatLng,
map: map,
title: "<div style='height:60px;width:200px'><b>Your location:</b><br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude,
});
google.maps.event.addListener(marker, "click", function (e) {
var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(marker.title);
infoWindow.open(map, marker);
// document.getElementById("latitude").value = p.coords.latitude;
// document.getElementById("longitude").value = p.coords.latitude;
});
var longitude = document.getElementById('latitude'),
latitude = document.getElementById('longitude');
function GetPosition(p) {
var long = p.coords.longitude,
lat = p.coords.latitude
longitude.text(long);
latitude.text(lat);
}
});
} else {
alert('Location feature is not supported in this browser.');
}
</script></code></pre>
</div>
</div>
</p>
<p>The body section is as follows:</p>
<p><div>
<div>
<pre><code> <div>
<div id="dvMap" style="width: 500px; height: 500px"></div>
<asp:Label ID="latitude" runat="server"></asp:Label>
<asp:Label ID="longitude" runat="server"></asp:Label>
</div>
Tried using GeoCoordinateWatcher without success. Is there an alternative method to acquire user location? Appreciate your help.