我正在尝试发出警告消息,如果用户没有共享其位置,则不应将纬度和经度空值插入数据库表。
JavaScript代码:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
alert("Geo Location is not supported on your current browser!");
}
function success(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
var city = position.coords.locality;
var myLatlng = new google.maps.LatLng(lat, long);
var myOptions = {
center: myLatlng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
title: "lat: " + lat + " long: " + long
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({ content: "<b>User Address</b><br/> Latitude:" + lat + "<br /> Longitude:" + long + "" });
document.getElementById("<%= latt.ClientID %>").value = position.coords.latitude;
document.getElementById("<%= lng.ClientID %>").value = position.coords.longitude;
var div = document.getElementById('map_canvas');
div.style.visibility = 'hidden';
infowindow.open(map, marker);
}
</script>
我的ASP.NET页面加载方法:
protected void Page_Load(object sender, EventArgs e)
{
var latt = this.latt.Value;
var lng = this.lng.Value;
ViewState["latt"] = latt.ToString();
ViewState["lng"] = lng.ToString();
}
ASP.NET HTML代码:
<asp:HiddenField ID="latt" runat="server" />
<asp:HiddenField ID="lng" runat="server" />
<asp:ImageButton ID="ImageButton_Attendance_login" runat="server" Visible="false"
ImageUrl="~/images/Attendance_login.png"
onclick="ImageButton_Attendance_login_Click"/>
我的按钮单击事件代码:
protected void ImageButton_Attendance_login_Click(object sender, ImageClickEventArgs e)
{
string Status = string.Empty;
bo.Para1 = Session["Userid"].ToString();//UserId
bo.Para2 = Session["Name"].ToString();//UserName
bo.Para3 = this.Page.Request.ServerVariables["REMOTE_ADDR"];//IPAddress
bo.Para4 = "";//City
bo.Para5 = ViewState["latt"].ToString();//Latitude
bo.Para6 = ViewState["lng"].ToString();//Longitude
bo.Para7 = attendancetype;//SessionType
bl.Insert_Branches_AttendanceInfo(bo, out Status);
if (Status == "Success")
{
// Response.Redirect("mypage.aspx");
// EmployeeAttendance(attendancetype);
// Response.Write("<script>alert('Thank You');</script>");
}
else
if (Status == "NotShareLocation")
{
Response.Write("<script>alert('Please Share Your Location');</script>");
}
}