javascript变量分配给asp.net

时间:2018-06-10 16:23:08

标签: javascript asp.net

这是我的脚本,用于获取纬度,经度和地名。我只能得到纬度而不是地名。



  <script type="text/javascript">
        $(document).ready(function () {
            var longitude = $('#longitude');
            var latitude = $('#latitude');
            var hiddenControl = '<%= inpHide.ClientID %>';
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (p) {
                    longitude.text(p.coords.longitude);
                    latitude.text(p.coords.latitude);
                    document.getElementById(hiddenControl).value = p.coords.latitude + "," + p.coords.longitude;
                    var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
                    //   alert(p.coords.latitude);
                    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);
                    });
                    var hidPlace = '<%= inpPlace.ClientID %>';
                     var lblplace = $('#lblplace');
                    var google_maps_geocoder = new google.maps.Geocoder();
                    google_maps_geocoder.geocode(
                        { 'latLng': LatLng },
                        function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK && results[0]) {
                                lblplace.text(results[0].formatted_address);
                            }
                        }
                    );

                });
            } else {
                alert('Location feature is not supported in this browser.');
            }
        });
    </script>
&#13;
&#13;
&#13;

&#13;
&#13;
 <section class="content">
        <script src="http://maps.google.com/maps/api/js?key=key"></script>
        <label id="latitude" runat="server"></label>
        <br />
        <label id="longitude" runat="server"></label>
        <br />
        <div id="dvMap" style="width: 500px; height: 500px" runat="server" visible="false"></div>
        <input id="inpHide" type="hidden" runat="server" />
        <input id="locHide" type="hidden" runat="server" />
         <label id="lblplace" runat="server"></label>
         <input id="inpPlace" type="hidden" runat="server" />
        <br />

    </section>
&#13;
&#13;
&#13;

后面的asp.net c#代码我可以得到经度作为纬度和经度,但lblplace每次都返回null值。请支持我找到我做错的地方。

隐藏字段

&#13;
&#13;
           var hidPlace = '<%= inpPlace.ClientID %>';
                     var lblplace ='<%= locHide.ClientID %>'
                    var google_maps_geocoder = new google.maps.Geocoder();
                    google_maps_geocoder.geocode(
                        { 'latLng': LatLng },
                        function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK && results[0]) {
                                document.getElementById(hidPlace).value=results[0].formatted_address;
                            }
                        }
                    );
&#13;
&#13;
&#13;

0 个答案:

没有答案