問題:問題在於,如果郵政編碼不是有效的,即實際的郵政編碼,頁面就會崩潰。在Javascript函數中處理錯誤
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>VEMap.Geocode Method</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3"></script>
<script type="text/javascript">
var map = null;
function GetMap() {
map = new VEMap('myMap');
map.LoadMap();
}
function ClickGeocode() {
map.Geocode(document.getElementById('txtQuery').value, findCallback);
}
function findCallback(layer, findResults, placeResults, moreResults, error) {
var s = '';
if (placeResults) {
for (var i = 0; i < placeResults.length; ++i) {
s += 'Name: ' + placeResults[i].Name + '\n';
s += 'LatLong: ' + placeResults[i].LatLong + '\n';
s += 'MatchCode: ' + placeResults[i].MatchCode + '\n';
s += 'MatchConfidence: ' + placeResults[i].MatchConfidence + '\n';
s += '\n\n';
}
}
else {
// Code was an error!
// Do something else!
console.log(error);
}
}
</script>
好的,你怎麼知道它是無效的? –
你能提供jsFiddle demo –
如果你改變郵編來說「12345XYZ」,它會引發錯誤。 @MattBurland – indofraiser