我正在使用google.api来验证地址,然后在地址有效的情况下进行ajax调用以插入数据库。但谷歌代码是在if((lati === undefined)&&(longi === undefined))之后执行的,其设置地址无效。有人可以建议为什么它在检查lati和longi for undefined之前没有执行google.map。
$( document ).ready(function() {
var geocoder = new google.maps.Geocoder(), lati,longi;
$("#editDistributor").dialog({
autoOpen: false,
height: 550,
width: 550,
modal: true,
buttons: {
"save": function() {
if ($( "#distAddress" ).val()) {tempAddress = $( "#distAddress" ).val();}
if($( "#distCity" ).val()) tempAddress = tempAddress + $( "#distCity" ).val();
if($( "#distState" ).val()) tempAddress = tempAddress + $( "#distState" ).val();
if($("#distZip").val()) tempAddress = tempAddress + $("#distZip").val();
geocoder.geocode({'address': tempAddress }, function(results, status) {
if(status) {
google.maps.GeocoderStatus.OK:
lati=results[0].geometry.location.k;
longi=results[0].geometry.location.D;
}
});
if ((lati === undefined) && (longi=== undefined)){
alert ( "Invalid Address");
}else{
var dialog = $(this);
var mapContent = new Object();
if($( "#distCompany" ).val() != undefined){
mapContent.name=$( "#distCompany" ).val();
}else{
alert("Please enter company Name");
}
$.ajax({
type: "POST",
url: window.location.origin+"/"+window.location.pathname.split("/")[1]+"/map/create",
data: JSON.stringify(mapContent),
dataType: "html",
contentType: 'application/json; charset=utf-8',
success: function(response){
alert("yes");
},
error: function(){
alert("Failure");
}
});
}
},
close: function() {
$(this).dialog("close");
}
}
});
感谢