0

首先,我想说我已经在互联网上寻找了很长时间并且一直无法找到答案,因此我的问题在这里。

我最近的学校项目是创建一个用于将文章添加到数据库的管理页面,这些文章连接到谷歌地图上的一个点。在地图上添加点的要求是用户能够单击一次地图并生成标记,如果第二次单击地图,则将第一个标记移动到第二个位置。(这就是我正在努力解决的问题。)

问题是,就像现在的代码一样,我收到标记数组未定义的错误。如果我放置 var markersArray = new Array; 在 eventListener 下方,然后我收到一个错误,提示 main.js(googles 文件)有问题,并且第二个 if 中未定义 markerArray[0]。

顺便说一句,我必须使用谷歌地图 API v2,即使它很旧。

    <script type="text/javascript">
//<![CDATA[

var map;
var markers = new Array;  

function load() {
  if (GBrowserIsCompatible()) {
    this.counter = 0;
    this.map = new GMap2(document.getElementById("map"));
    this.map.addControl(new GSmallMapControl());
    this.map.addControl(new GMapTypeControl());
    this.map.setCenter(new GLatLng(57.668911, 15.203247), 7);       

    GDownloadUrl("genxml.php", function(data) {
      var xml = GXml.parse(data);
      var Articles = xml.documentElement.getElementsByTagName("article");
      for (var i = 0; i < Articles.length; i++) {
        var id = Articles[i].getAttribute("id");
        var title = Articles[i].getAttribute("title");
        var text = Articles[i].getAttribute("text");
        var searchWord = Articles[i].getAttribute("searchWord");
        var point = new GLatLng(parseFloat(Articles[i].getAttribute("lat")),
                                parseFloat(Articles[i].getAttribute("lng")));
        var article = createMarker(point, id, title, text);
        this.map.addOverlay(article);           
      }
    });
  }

var myEventListener = GEvent.bind(this.map,"click", this, function(overlay, latlng) {

  if (this.counter == 0) {
    if (latlng) {
        var marker = new GMarker(latlng);
        latlng1 = latlng;
        this.map.addOverlay(marker);
        this.counter++;
        markers.push(marker); //This is where I get the error that markersArray is undefined.

  } 
} 
else if (this.counter == 1) {
  if (latlng){
    alert (markers[0]);
    this.map.removeOverlay(markers[0]);
    var markers = [];
    this.map.addOverlay(marker);
    this.counter++;
  }
} 

}); 
}

function createMarker(point, id, title, text) {
  var article = new GMarker(point);
  var html = "<b>" + title + "</b> <br/>"
  GEvent.addListener(article, 'click', function() {
    window.location = "article.php?id=" + id;

  });
  return article;
}
4

1 回答 1

0

我解决了这个问题。我不确定它为什么会起作用,但这就是现在的样子:

var markersArray = [];

function load() {
if (GBrowserIsCompatible()) {
    this.counter = 0;
    this.map = new GMap2(document.getElementById("map"));
    this.map.addControl(new GSmallMapControl());
    this.map.addControl(new GMapTypeControl());
    this.map.setCenter(new GLatLng(57.668911, 15.203247), 7);       

GDownloadUrl("genxml.php", function(data) {
var xml = GXml.parse(data);
var Articles = xml.documentElement.getElementsByTagName("article");
    for (var i = 0; i < Articles.length; i++) {
        var id = Articles[i].getAttribute("id");
        var title = Articles[i].getAttribute("title");
        var text = Articles[i].getAttribute("text");
        var searchWord = Articles[i].getAttribute("searchWord");
        var type = Articles[i].getAttribute("type");
        var point = new GLatLng(parseFloat(Articles[i].getAttribute("lat")),
                                parseFloat(Articles[i].getAttribute("lng")));
        var article = createMarker(point, id, title, text);
        this.map.addOverlay(article);           
      }
    });
  }

var myEventListener = GEvent.bind(this.map,"click", this, function(overlay, latlng) {
var marker = new GMarker(latlng);
if (this.counter == 0) {
    if (latlng) {
        latlng1 = latlng;
        this.map.addOverlay(marker);    
        markersArray.push(marker);      
        this.counter++; 

  } 
 } 
else if (this.counter == 1) {
    if (latlng){
        this.map.removeOverlay(markersArray[0]);
        this.map.addOverlay(marker);
        this.counter++;
  }
} 

}); 
}

function createMarker(point, id, title, text) {
var article = new GMarker(point);
var html = "<b>" + title + "</b> <br/>"
GEvent.addListener(article, 'click', function() {
    window.location = "article.php?id=" + id;
});
return article;
}
于 2012-05-30T19:21:37.320 回答