将 Google 地图集中在数据库中的点上?

发布于 2024-09-01 07:07:25 字数 1993 浏览 3 评论 0原文

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

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

          }
      });
    }

如何更改纬度此行上的 Lng 值

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

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

I'm trying to center a google map on a single marker pulled from a database, what I'm doing so far is using this function to get a single marker from the database

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);

    }

and this function to load the map

    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

          }
      });
    }

How can I change the Lat Lng values on this line

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

to match the lat lng values from the single marker? or is there a simpler way to pull a single marker from a database and center the map on it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-09-08 07:07:25

如果 getMarkers() 仅检索一个标记,您似乎可以简单地添加:

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

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

map.addOverlay(markers[i]);

If getMarkers() is retrieving just one marker, it looks like you can simply add:

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

After or before you add the marker to the map:

map.addOverlay(markers[i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文