Hi surzo4368,
Refer the below code. Create Action for saving data to database with parameter string lat, string lng and make ajax call to save upon user accepting to share his or her locaiton.
<script type="text/javascript">
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (p) {
// Ajax call start to save userlocation in database upon accepting to share his or her locaiton.
$.ajax({
type: "POST",
url: "Home/SaveCurrentLocation/",
data: '{ lat :' + p.coords.latitude + ', lng :' + p.coords.longitude + ' }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Location added successfully.");
},
error: function (response) {
alert(response.responseText);
}
});
// Ajax call End
var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
var mapOptions = { center: LatLng, zoom: 13, 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>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);
});
});
} else {
alert('Geo Location feature is not supported in this browser.');
}
</script>