简体   繁体   English

如何在每1分钟加载后保持我在地图中的最后位置

[英]how to stay my last position in map after every 1 min load

how to track the current position on map when i load map at every 1 min but right now map position redirect to default position after every 1 min. 当我每1分钟加载地图时如何跟踪地图上的当前位置,但是现在每1分钟后地图位置重定向到默认位置。

here is my code.i call load function at every 1 min and i fetch data from mapajax1.php file.i want to stay my last position on map because i track the driver 这是我的code.i每1分钟调用加载函数,我从mapajax1.php文件中获取数据。我希望保留我在地图上的最后位置,因为我跟踪驱动程序

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"></script>
    <script type="text/javascript">
     function load() {

          var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng("<?php echo $lat;?>", "<?php echo $lng;?>"),
            zoom: 5,
            mapTypeId: 'roadmap'
          });
          var infoWindow = new google.maps.InfoWindow;

          // Change this depending on the name of your PHP file
          downloadUrl("mapajax1.php", function(data) {
            var xml = data.responseXML;


            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
              var name = markers[i].getAttribute("name");
              var email = markers[i].getAttribute("email");
              var phone = markers[i].getAttribute("phone");
              var status = markers[i].getAttribute("status");
              var type = markers[i].getAttribute("type");
              var point = new google.maps.LatLng(
                  parseFloat(markers[i].getAttribute("lat")),
                  parseFloat(markers[i].getAttribute("lng")));
              var html = "<b>" + name + "</b> <br/>" + email+ "</b> <br/>" + phone+ "</b> <br/>" + status;
              var icon = customIcons[type] || {};
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon
              });
              bindInfoWindow(marker, map, infoWindow, html);
            }
          });
        }

     setInterval(function(){ 
                load()    
            }, 60000);
    </script>

You should set a default location for your app. 您应该为应用设置默认位置。 How about you use your own location? 你怎么用自己的位置? HTML5 has geolocation support. HTML5具有地理定位支持。 This means modern browsers can find your location by your IP address. 这意味着现代浏览器可以通过您的IP地址找到您的位置。 See this link for detailed information. 有关详细信息,请参阅此链接 This is the basic snippet of code which will display your coordinates in the dev console of your browser. 这是代码的基本代码段,它将在浏览器的开发控制台中显示您的坐标。

var lat = null;
var lng = null;

// sets your location as default
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    var locationMarker = null;
    if (locationMarker){
      // return if there is a locationMarker bug
      return;
    }

    lat = position.coords["latitude"];
    lng = position.coords["longitude"];

   console.log(lat, lng);

  },
  function(error) {
    console.log("Error: ", error);
  },
  {
    enableHighAccuracy: true
  }
  );
}

You need to save the last position on the map before calling load() function. 在调用load()函数之前,需要在地图上保存最后一个位置。 your code returns to initial position every time because of that. 因此,您的代码每次都会返回初始位置。

here is a sample code, the map is loaded every 5 seconds with the last postion on the map. 这是一个示例代码,地图每5秒加载一次,地图上有最后一个位置。

jsfiddle link jsfiddle链接

var lat = -27.4; //<?php echo $lat;?>
var lng = 153.023; //<?php echo $lng;?>

function load() {

  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(lat, lng),
    zoom: 5,
    mapTypeId: 'roadmap'
  });
  //....... rest of code
  map.addListener('center_changed', function() {
    lat = map.getCenter().lat();
    lng = map.getCenter().lng();
  });
}

setInterval(function() {
  load()
}, 5000);

When creating the map, add a listener to update the lat and lng 创建地图时,添加一个侦听器以更新lat和lng

  map.addListener('center_changed', function() {
    lat = map.getCenter().lat();
    lng = map.getCenter().lng();
  });

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

相关问题 如何在包含地图的每 1 分钟后自动刷新 div - how to auto refresh div after every 1 min that contain map 如何使用Javascript或jQuery刷新后不停留在屏幕的相同位置? - How to use Javascript or jQuery to stay at the same position of my screen not going up after refreshing? Angular-Google-Map:如何在加载位置数据后加载地图(Lat,Lng)? - Angular-Google-Map: How to Load Map after Position Data Loaded(Lat, Lng)? 提交html表单后如何保持原样? - How to stay on same position after submitting an html form? 如何使我的按钮保持在静止位置 - How to make my button stay in its stationary position 如何使滑块控件保持在同一位置 - How can i make my slider controls stay at the same position 如何在 array.map 中除 React JSX 中的最后一个元素之外的每个元素之后添加逗号 - How to add a comma in array.map after every element except last element in React JSX 运行item.url时的方法.map与视图的每次迭代都保留最后一个url,同时保持每个视图的更改 - method .map when running item.url stay with the last url on every iteration of the view while keeping changes from each of it 如何使使用Google Javascript Map API创建的地图加载并保持白色 - How to make a map made with Google Javascript Map API load and not stay white 我如何在 reactjs 每 5 分钟后调度一个动作 - How can I dispatch an action after every 5 min in reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM