简体   繁体   English

如何将JavaScript警报消息值传递给asp.net中的标签

[英]How to pass javascript alert message value to label in asp.net

I had implemented geolocation , wherein browser gets location of user. 我实现了geolocation,其中浏览器获取用户的位置。 In this code i'm getting geolocation of user through browser ,but it display location using javascript alert message. 在这段代码中,我通过浏览器获取用户的地理位置,但是它使用javascript警报消息显示位置。

I just want to pass alert message value to label or hiddenfield in ASP.Net & doesn't want to run alert message 我只想将警报消息值传递给ASP.Net中的标签或hiddenfield,并且不想运行警报消息

My javascript code as follows. 我的JavaScript代码如下。

<script type="text/javascript">
     var geocoder;

     if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
     }
     //Get the latitude and the longitude;
     function successFunction(position) {
         var lat = position.coords.latitude;
         var lng = position.coords.longitude;
         codeLatLng(lat, lng)
     }

     function errorFunction() {
         alert("Geocoder failed");
     }

     function initialize() {
         geocoder = new google.maps.Geocoder();
     }

     function codeLatLng(lat, lng) {

         var latlng = new google.maps.LatLng(lat, lng);
         geocoder.geocode({ 'latLng': latlng }, function (results, status) {
             if (status == google.maps.GeocoderStatus.OK) {
                 console.log(results)
                 if (results[1]) {
                     //formatted address
                     alert(results[0].formatted_address)
                     //find country name
                     for (var i = 0; i < results[0].address_components.length; i++) {
                         for (var b = 0; b < results[0].address_components[i].types.length; b++) {

                             //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                             if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                                 //this is the object you are looking for
                                 city = results[0].address_components[i];
                                 break;
                             }
                         }
                     }
                     //city data
                     alert(city.short_name + " " + city.long_name)
                      document.getElementById('<%=Label1.ClientID %>').innerHTML = city;

                 } else {
                     alert("No results found");
                 }
             } else {
                 alert("Geocoder failed due to: " + status);
             }
         });
     }
</script> 

If you want to store the value in a hidden field replace 如果要将值存储在隐藏字段中,请替换

alert(results[0].formatted_address)

with

//hdnField is the ID of your asp:HiddenField object
var hiddenField = document.getElementById("<%= hdnField.ClientID %>")
hiddenField.value = results[0].formatted_address;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM