IF条件下的Google地图

时间:2014-08-25 08:34:06

标签: javascript jquery html google-maps-api-3

我有一个场景,谷歌地图加载来自数据库或使用也来自数据库的邮政编码的经度和纬度,如果long - lat变为null,这是无效的。我将long,lat,Pin代码值绑定到隐藏字段。

脚本是:

var lat = $('#<%=hdnLat.ClientID %>').val();
var long = $('#<%=hdnLong.ClientID %>').val();
var add = $('#<%=hdnAddress.ClientID %>').val();
var title = $('#<%=hdnTitle.ClientID %>').val();
var pinCode = $('#<%=hdnPin.ClientID %>').val();

if (!lat == '' || !lat == null) {
    function initialize() {
        var latlng = new google.maps.LatLng(lat, long);
        var zoomratio = 15;
        var mapOptions = {
            center: latlng,
            zoom: zoomratio,
            scrollwheel: true,
            draggable: true,
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                position: google.maps.ControlPosition.RIGHT_TOP
            },
            panControl: false,
            zoomControl: true,
            streetViewControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL,
                position: google.maps.ControlPosition.RIGHT_BOTTOM
            }
        };
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
        var url = "Images/YellowPages/pointer.png";
        var htmlStr = "<div style='border:1px solid #ddd;padding:3px;'>" +
           "<h4 style='margin-top:0;width:200px;line-height:1.25;'>" + title + "</h4>" +
           "<p>" + add + "</p>" +
           "</div>";
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: url,
            title: title
        });
        (function (marker, htmlStr) {
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(htmlStr);
                infoWindow.open(map, marker);
            });
        })(marker, htmlStr);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
} else {
    function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(12.9539974, 77.6309395);
        var zoomratio = 8;
        var mapOptions = {
            center: latlng,
            zoom: zoomratio,
            scrollwheel: true,
            draggable: true,
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                position: google.maps.ControlPosition.RIGHT_BOTTOM
            },
            panControl: false,
            zoomControl: true,
            streetViewControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL,
                position: google.maps.ControlPosition.RIGHT_BOTTOM
            }
        }
        map = new google.maps.Map(document.getElementById('googlemap'), mapOptions);
        codeAddress();
    }

    function codeAddress() {

        geocoder.geocode({ 'address': pinCode + ', India' }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
}

问题是google.maps.event.addDomListener(window, 'load', initialize);始终首先设置为第一个条件,然后如果latlong变量中有值,则立即跳转到第二个条件。

无论如何都要避免它,或者有什么简单的方法吗?

1 个答案:

答案 0 :(得分:1)

您的条件有问题

if(!lat == '' || !lat == null)
总是评估为TRUE的

(因为!lat表示为FALSE)

你打算写的是:

if(lat != '' || lat != null)