简体   繁体   English

谷歌地图api v3自动中心自动缩放

[英]google maps api v3 auto center auto zoom

This is based on the Google Maps API v3. 这基于Google Maps API v3。 The page refreshes every 10 seconds and updates the marker positions on the map with auto centre and auto zoom. 页面每10秒刷新一次,并使用自动居中和自动缩放更新地图上的标记位置。 The following are the issues that I face: 以下是我面临的问题:

  1. Every time the page loads(every 10 seconds) map is centered to (47.6145, -122.3418) initially and then centers and zoom based on the markers positions. 每次页面加载(每10秒)时,地图最初以(47.6145,-122.3418)为中心,然后根据标记位置居中和缩放。 If uncomment the following the map doesn't load at all: 如果取消注释以下内容,则根本不会加载地图:

center: new google.maps.LatLng(47.6145, -122.3418), center:new google.maps.LatLng(47.6145,-122.3418),

How can I stop the map loading initial lat and long coordinates (47.6145, -122.3418) every time the map refreshes. 每次地图刷新时,如何停止地图加载初始纬度和长坐标(47.6145,-122.3418)。

2. Is there a way to control the zoom levels? 2.有没有办法控制缩放级别?

Here is the code based on the google maps api v3 这是基于谷歌地图api v3的代码

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 14,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  downloadUrl("phpsqlajax_genxml2.php", function(data) {

    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    var bounds  = new google.maps.LatLngBounds();
    for (var i = 0; i < markers.length; i++) {
      var UID = markers[i].getAttribute("UID");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("latitude")),
          parseFloat(markers[i].getAttribute("longitude")));
      var html = "<b>" + UID + "</b> <br/>";
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
    bounds.extend(point);
      bindInfoWindow(marker, map, infoWindow, html);
    }
   map.fitBounds(bounds);
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}


  setInterval(load, 10000); 

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

Firstly to control the zoom level there is a method called setZoom() that you can call on the maps object, in your example the map variable. 首先要控制缩放级别,有一个名为setZoom()的方法,您可以在地图对象上调用,在您的示例中为map变量。

map.setZoom(5);

but more to your other problem of the map refreshing. 但更多的是你的地图刷新的另一个问题。 I'm assuming that the whole webpage is refreshing to update the content of the map - I think you should investigate ajax calls to refresh the data in the map without refreshing the whole page 我假设整个网页都刷新了更新地图的内容 - 我认为你应该调查ajax调用来刷新地图中的数据而不刷新整个页面

You already have the base by using the downloadUrl() method - now just put that on a setInterval() and let the data refresh every 30 seconds to 1 minute. 你已经有了使用downloadUrl()方法的基础 - 现在只需将它放在setInterval() ,让数据每30秒刷新1分钟。

I would also suggest that you clear the map of the markers before you add new ones otherwise you will have a memory leak creating duplicate DOM elements everytime the data refreshes. 我还建议您在添加新标记之前清除标记的映射,否则每次数据刷新时都会产生内存泄漏,从而创建重复的DOM元素。 I usually put my marker references in an array and then loop through the array and run marker.setMap(null) which will remove the marker from the map. 我通常将我的标记引用放在一个数组中,然后遍历数组并运行marker.setMap(null) ,它将从地图中删除标记。

EDIT with code snippet 使用代码段编辑

using your code snippet above lets try and make this work by seperating the code into separate functions a load function and a refresh function, also make sure you move map and infoWindow into the global scope. 使用上面的代码片段让我们尝试通过将代码分离到单独的函数,加载函数和刷新函数来完成这项工作,同时确保将mapinfoWindow移动到全局范围。

var map;
var infoWindow;
function load() {
    map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 14,
    mapTypeId: 'roadmap'
    });
    infoWindow = new google.maps.InfoWindow;
    refresh();
}
function refresh(){

    downloadUrl("phpsqlajax_genxml2.php", function(data) {

    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    var bounds  = new google.maps.LatLngBounds();
    for (var i = 0; i < markers.length; i++) {
      var UID = markers[i].getAttribute("UID");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("latitude")),
          parseFloat(markers[i].getAttribute("longitude")));
      var html = "<b>" + UID + "</b> <br/>";
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
    bounds.extend(point);
      bindInfoWindow(marker, map, infoWindow, html);
    }
   map.fitBounds(bounds);
  });
}
setInterval("refresh()",60000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM