gpt4 book ai didi

javascript - IF 条件下的 Google map

转载 作者:行者123 更新时间:2023-12-03 11:59:07 27 4
gpt4 key购买 nike

我有一个场景,如果长纬度变为空,则谷歌地图会加载来自数据库的经度和纬度,或者使用也来自数据库的邮政编码。我将长、纬度、Pin 代码值绑定(bind)到隐藏字段。

脚本是:

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 变量。

有什么办法可以避免它或者有什么简单的方法吗?

最佳答案

您的情况有问题

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

始终评估为 TRUE(因为 !lat 表示 FALSE)

你想写的是:

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

关于javascript - IF 条件下的 Google map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25481960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com