找到用户的位置并在一个帖子中弹出显示

时间:2016-03-16 06:58:55

标签: javascript c# asp.net .net c#-4.0

需要显示用户的地理位置(ZipCode)并在页面加载期间显示它。目前我通过2个帖子实现了这个目标,有没有办法在单个帖子中实现。

由于

我尝试过:

function ShowMessages() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success);
    } else {
        alert("Geo Location is not supported on your current browser!");
    }
    function success(position) {
        debugger;
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = geocoder = new google.maps.Geocoder();
        geocoder.geocode({'latLng': latlng},
            function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        var searchAddressComponents = results[0].address_components,
                            searchPostalCode = "";
                        $.each(searchAddressComponents, function () {
                            if (this.types[0] == "postal_code") {
                                searchPostalCode = this.short_name;
                            }
                        });

                        document.getElementById('hidden1').value = searchPostalCode
                        __doPostBack('', '');


                    }
                }
            });
    }

}

1 个答案:

答案 0 :(得分:0)

如果你能找到searchPostalCode的值,那么将它显示为span,该跨度将移动到被填充的div上。

document.getElementById('hidden1').value = searchPostalCode;
                    document.getElementById('YourSpan').innerText = searchPostalCode;
                    $("#YourDiv").append($("#YourSpan"));
                    $("#YourDiv").modal();//Apply your modal code here