目前程序正在运行,但是由于alert()
我在函数中使用的getData()
函数,界面很烦人!当我从函数中删除这一行时,getData()
整个程序出错了!!我不知道是什么问题?有人有更好的主意来做这样的过程吗?
我在这里尝试制作的程序旨在帮助用户找到距当前地址50公里内的餐厅,我已经收集了各种位置地址并将其记录在数据库中。
initialize()
加载 HTML 正文时调用的函数,在 HTML 正文的第一行中,餐厅数据将使用 PHP 从 MySQL 中提取,PHP 会将数据打印到 JavaScript 数组 jsres_add、jsres_id、jsres_name 和 jsnu 中,以便我可以在 JavaScript 代码中使用它们。*请注意下面的JavaScript代码是分开在.js文件中的
var geocoder, location1, location2, gDir, oMap, jsnu, arraynu, address2;
jsres_add = new Array();
jsres_id = new Array();
jsres_name = new Array();
function initialize() {
geocoder = new GClientGeocoder();
gDir = new GDirections();
GEvent.addListener(gDir, "load", function() {
var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
if (drivingDistanceKilometers < 50){
// function to save search result within 50km into database using ajax
saveSearchResult(jsres_id[arraynu],jsres_name[arraynu],drivingDistanceKilometers);
}
});
}
function getData() {
emptytable(); //function to empty search result table using ajax
//jsnu is the number of the restaurants data found in database
for (var ii = 0; ii < jsnu; ii++) {
arraynu = ii;
address2 = jsres_add[ii];
showLocation();
alert("done!");
}
showResults(); //function to print out the search result from database into html body using ajax
}
function showLocation() {
geocoder.getLocations(document.forms[0].address1.value, function (response) {
if (!response || response.Status.code != 200){
alert("Sorry, we were unable to geocode your address");
}else{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(address2, function (response) {
if (!response || response.Status.code != 200){
alert("Sorry, we were unable to geocode the second address");
}else{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
}
});
}
});
}