如何在不刷新整个地图的情况下更新谷歌地图引脚?

时间:2011-12-29 20:35:39

标签: php javascript google-maps

我一直在尝试制作一张可在不同位置显示多个标记的Google地图。此地图连接到存储纬度和经度的数据库。我想制作这张地图,以便我可以更改数据库中的纬度和经度,以便标记可以更新并重新定位到新位置。但我不希望整个地图重新加载,只是数据。这是代码,谢谢你的帮助!

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

var iconBlue = new GIcon(); 
iconBlue.image = 'mm_20_blue.png';
iconBlue.shadow = 'mm_20_shadow.png';
iconBlue.iconSize = new GSize(16, 25);
iconBlue.shadowSize = new GSize(27.5, 25);
iconBlue.iconAnchor = new GPoint(7.5, 25);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon(); 
iconRed.image = 'mm_20_red.png';
iconRed.shadow = 'mm_20_shadow.png';
iconRed.iconSize = new GSize(16, 25);
iconRed.shadowSize = new GSize(27.5, 25);
iconRed.iconAnchor = new GPoint(7.5, 25);
iconRed.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
customIcons["1"] = iconBlue;
customIcons["2"] = iconRed;

function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.setUIToDefault();
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(34.081187, -83.980721), 8);
    geocoder = new GClientGeocoder();


    GDownloadUrl("genxml2.php", function(data) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var Names = markers[i].getAttribute("Names");
        var phoneNum = markers[i].getAttribute("phoneNum");
        var gender = markers[i].getAttribute("gender");
        var homeAd = markers[i].getAttribute("homeAd");
        var lat = markers[i].getAttribute("latitudeE6")
        var lng = markers[i].getAttribute("longitudeE6")
        var point = new GLatLng(parseFloat(markers[i].getAttribute("latitudeE6")),
                                parseFloat(markers[i].getAttribute("longitudeE6")));
        var marker = createMarker(point, Names, phoneNum, gender, homeAd, lat, lng);
        map.addOverlay(marker);
      }
    }
    );
  }
}

function createMarker(point, Names, phoneNum, gender, homeAd, lat, lng) {
  var marker = new GMarker(point, customIcons[gender]);
  place = marker

  var html = "<b>Name: " + Names + "</b> <br/>Phone #: " + phoneNum + "</b> <br/>Home Address: " + homeAd + "</b> <br/>Current Address: " + place.address
  + "</b> <br/>Latitude: " + lat + "</b> <br/>Longitude: " + lng;
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
};

//]]>

   

(代码的某些部分被排除在外,如MD5密钥)

2 个答案:

答案 0 :(得分:0)

首先创建一个名为load_marker的函数

将您的代码放入新函数中启动gdownloadurl,并在调用此javascript函数时更改数据时加载标记。您可能需要先清除所有标记。

您也可以在地图事件中重新加载标记。您可以使用两个有用的事件。

drag_end和zoom_changed。你可以通过gmap.registerEvent函数注册这些

编辑:添加代码示例/您的代码应该是这样的。

function load() {
 if (GBrowserIsCompatible()) {
 var map = new GMap2(document.getElementById("map"));
 map.setUIToDefault();
 map.addControl(new GMapTypeControl());
 map.setCenter(new GLatLng(34.081187, -83.980721), 8);
 geocoder = new GClientGeocoder();
}

function load_markers() {
/*clear all*/
map.clearOverlays();

GDownloadUrl("genxml2.php", function(data) {
  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");
  for (var i = 0; i < markers.length; i++) {
    var Names = markers[i].getAttribute("Names");
    var phoneNum = markers[i].getAttribute("phoneNum");
    var gender = markers[i].getAttribute("gender");
    var homeAd = markers[i].getAttribute("homeAd");
    var lat = markers[i].getAttribute("latitudeE6")
    var lng = markers[i].getAttribute("longitudeE6")
    var point = new GLatLng(parseFloat(markers[i].getAttribute("latitudeE6")),
                            parseFloat(markers[i].getAttribute("longitudeE6")));
    var marker = createMarker(point, Names, phoneNum, gender, homeAd, lat, lng);
    map.addOverlay(marker);
  }
}
);
}
}

答案 1 :(得分:0)

您应该切换到Google Maps API v3,其中指针链接到地图而不是相反。另外,您将免费获得一系列有趣的新功能!