从数据库点中心谷歌地图?

时间:2010-05-11 13:08:09

标签: javascript api google-maps geolocation

我正试图将谷歌地图集中在从数据库中提取的单个标记上,到目前为止我正在使用此功能从数据库中获取单个标记

function getMarkers(){
        var urlstr="readsingle.php?v=<?php echo $cam ?>";
        var request = GXmlHttp.create();
        request.open('GET', urlstr , true); // request XML from PHP with AJAX call
        request.onreadystatechange = function () {
            if (request.readyState == 4) { // 4 is a completed request
                var xmlDoc = request.responseXML;
                locations = xmlDoc.documentElement.getElementsByTagName("location");
                markers = [];
                if (locations.length){
                    for (var i = 0; i < locations.length; i++) { // cycle thru locations
                        markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
                        markers[i].infowindow = "This is "+locations[i].getAttribute("name");
                        markers[i].markerindex = i;
                        markers[i].db_id = locations[i].getAttribute("location_id");
                        map.addOverlay(markers[i]);

                    }
                }
            }
        }

        request.send(null);

    }

和此函数加载地图

    function onLoad() {
      map = new GMap(document.getElementById("div_map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(53.76221, -2.71227), 8);
      map.enableScrollWheelZoom();
      getMarkers();
      GEvent.addListener(map, "click", function(overlay, point) {
          if (overlay){ // marker clicked
              overlay.openInfoWindowHtml(overlay.infowindow);   // open InfoWindow
          } else if (point) {   // background clicked

          }
      });
    }

如何更改此行上的Lat Lng值

map.setCenter(new GLatLng(53.76, -2.71), 5); 

匹配单个标记的lat lng值?或者是否有更简单的方法从数据库中提取单个标记并将地图置于其中心?

1 个答案:

答案 0 :(得分:1)

如果getMarkers()只检索一个标记,则看起来您只需添加:

map.setCenter(new GLatLng(locations[i].getAttribute("lat"), 
                          locations[i].getAttribute("lng")), 5);

在将标记添加到地图之后或之前:

map.addOverlay(markers[i]);