在我的代码中 - 当我加载应用程序时,我应该使用第一页的 DATATABLE 中的地址进入地图标记,当我按下下一个按钮时,我应该得到第二个的地址,依此类推。
当我应该执行此代码时,问题出在 onload 情况下
else
{
glPNum = (numberOfPages * (s-1)+1);
for (i=0;i<intPageSize;i++)
{ alert (intPageSize);
//alert (arrayMyAddresses[i]);
geocoder = new GClientGeocoder();
geocoder.getLocations(arrayMyAddresses[i], addAddressToMap);
}
addmarkers();
}
应用程序的输出是它给了我警报批次:0 这意味着我的 Gmarker 元素数组为 0,并且它尝试执行函数 addmarkers 并且它不会向地图添加标记,因为数组批次的长度如你所见 0
然后它开始提醒addresstomap batch 1 adresstomap batch 2 etc,这意味着现在它在函数addAddressToMap中。为什么它会这样!奇怪的是,当我按下下一个按钮并且我有页面事件时,程序正在运行,它会在地图上添加标记
对不起我的英语不好我希望你能理解
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
} else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var intRpp=100;
var intPNum;
var glPNum;
var s;
var options = {'showRowNumber': true, 'pageSize':intPageSize };
var numberOfPages;
var intPageSize=10;
var holde = 7;
var map ;
var geocoder;
var actualepage = 0 ;
var arrayMyAddresses = new Array();
var batch = new Array();
var mgr;
function initialize()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(42.143365, 24.751032), 13);
map.setUIToDefault();
/*var mrkCordinates = new GLatLng(42.129539,24.741726);
var marker = new GMarker(mrkCordinates);
marker.openInfoWindowHtml("Your cordinates" + mrkCordinates.lat());
map.addOverlay(marker);*/
}
}
function addmarkers()
{
/* for (i=0;i<batch.length;i++)
map.addOverlay(batch[i]);*/
alert("batch: " + batch.length);
mgr = new MarkerManager(map);
mgr.addMarkers(batch,10);
mgr.refresh();
}
function clearmarkers()
{ batch=[];
alert ("you are about to clear");
mgr.clearMarkers();
mgr.refresh();
}
function addAddressToMap(response)
{
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode that address");
}
else
{
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
var marker = new GMarker(point);
batch.push(marker);
alert ("addresstomap batch " + batch.length);
}
}
function addOpenWindow(response)
{
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode that address");
}
else
{ place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
for (i=0;i<batch.length;i++)
{
if ((point.lat() == batch[i].getLatLng().lat())&&(point.lng() == batch[i].getLatLng().lng()))
{
batch[i].openInfoWindowHtml("Here is the address of your firm");
}
}
}
}
function setNumberOfPages(value)
{
options['pageSize'] = parseInt(value, 10);
intPageSize = parseInt(value, 10);
numberOfPages = intRpp/parseInt(value);
//alert (numberOfPages);
visual.draw(tblTst, options);
}
function loadXMLDoc(l)
{
arrayMyAddresses = [];
intPNum=l;
s=l;
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
var xmlObj = xmlhttp.responseXML;
var textXML = xmlObj.documentElement.firstChild.firstChild.nodeValue;
if (window.DOMParser)
{
parser=new DOMParser();
var xmlDoc=parser.parseFromString(textXML,"text/xml");
}
else // Internet Explorer
{
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(textXML);
}
var rec = new Array();
rec[0]=['Стопански субект', 'Юрид.форма', 'Община', 'Име', 'Роля', 'Страна', 'NACE code', 'NACE text', 'Селище', 'пощ.код','Адрес', 'тел.', 'факс', 'Email', 'web', 'rid','id','activ'];
var rows = xmlDoc.getElementsByTagName("row");
rowsn = rows.length;
for (i=1;i<=rowsn;i++)
{
var cols=rows[i-1].getElementsByTagName("colunm");
colsn=cols.length;
rec[i] = new Array();
for (var j=0; j<colsn; j++)
{
rec[i][j] = cols[j].getAttribute("colvalue");
}
rec[i][j]='<input type="button" onClick="ajaxDBDelete('+rec[i][15]+');"/>';
arrayMyAddresses.push(rec[i][8]+","+rec[i][10]);
}
tblTst = google.visualization.arrayToDataTable(rec);
options['page'] = 'event';
options['pageSize'] = intPageSize;
options['pagingSymbols'] = {prev: 'prev', next: 'next'};
options['pagingButtonsConfiguration'] = 'both';
options['allowHtml'] = 'true';
numberOfPages = intRpp/intPageSize;
visual = new google.visualization.Table(document.getElementById('table'));
google.visualization.events.addListener(visual, 'select', selectHandler);
function selectHandler(e)
{
var selection = visual.getSelection();
var message = '';
for (var i = 0; i < selection.length; i++)
{
var item = selection[i];
if (item.row != null)
{
var str = tblTst.getFormattedValue(item.row, 8);
str += ",";
str += tblTst.getFormattedValue(item.row, 10);
message = str;
}
}
if (message == '')
{
message = 'nothing';
}
alert (message);
geocoder = new GClientGeocoder();
geocoder.getLocations(message, addOpenWindow);
}
google.visualization.events.addListener(visual, 'page', function (e)
{
clearmarkers();
options.startPage = e.page;
//alert (e.page);
if (e.page == numberOfPages)
{
loadXMLDoc(s+1);
options.startPage = 0;
}
if ((e.page == -1)&&(s>1))
{
holde = e.page;
//alert (holde);
loadXMLDoc(s-1);
options.startPage=numberOfPages-1;
actualepage = e.page + numberOfPages;
}
else
{
if ((e.page+1) <= numberOfPages)
{
glPNum = (numberOfPages * (s-1)) + (e.page+1);
document.getElementById('txbNumPage').value = glPNum;
visual.draw(tblTst, options);
var k=(intPageSize * e.page);
for (i=k;i<intPageSize*(e.page+1);i++)
{
alert (arrayMyAddresses[i]);
//var sendaddrss =arrayMyAddresses[i]);
geocoder = new GClientGeocoder();
geocoder.getLocations(arrayMyAddresses[i], addAddressToMap);
}
addmarkers();
}
}
});
//alert (holde);
visual.draw(tblTst, options);
if (holde == -1)
{
glPNum = numberOfPages * s;
var f = actualepage * intPageSize;
for (i=f;i<(f+intPageSize);i++)
{
alert (arrayMyAddresses[i]);
geocoder = new GClientGeocoder();
geocoder.getLocations(arrayMyAddresses[i], addAddressToMap);
}
addmarkers();
holde=0;
}
else
{
glPNum = (numberOfPages * (s-1)+1);
for (i=0;i<intPageSize;i++)
{ alert (intPageSize);
//alert (arrayMyAddresses[i]);
geocoder = new GClientGeocoder();
geocoder.getLocations(arrayMyAddresses[i], addAddressToMap);
}
addmarkers();
}
document.getElementById('txbNumPage').value = glPNum;
}
}
var url = "http://localhost:9091/localbusscat/services/localbusscat/SelectFromDB?intRpp="+ intRpp+"&pageNum="+intPNum;
alert (url);
xmlhttp.open("GET", url, true);
xmlhttp.send();
}