简体   繁体   English

从php文件在谷歌地图中刷新经纬度

[英]Refresh lat long in google map from php file

i am new in stackoverflow and programming first time hello how can i refresh a php data to javascript google map lat long bicos the lat long ar change every second and i wood like to refresh lat long like realtime but the problem is thets load only the first time the real position and refresh only the same lat long.我是 stackoverflow 和编程的新手,你好,我如何将 php 数据刷新到 javascript google map lat long bicos lat long ar 每秒都在变化,我喜欢像实时一样刷新 lat long 但问题是他们只加载第一个计算真实位置的时间并仅刷新相同的经纬度。 how to load lat lon from php without reload the page如何在不重新加载页面的情况下从 php 加载经纬度

in this file is the php and this code down wher load google maps在这个文件中是 php 和加载谷歌地图的代码

    <script>
  function initMap() {
  var myLatlng = new google.maps.LatLng('.json_encode($Lat).', '.json_encode($Long).');
var mapOptions = {
zoom: 13,
center: myLatlng
    }
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
    });

marker.setMap(map);
setInterval( function(){ 

    marker.setPosition( new google.maps.LatLng( '.json_encode($Lat).', '.json_encode($Long).' ) );
    map.panTo( new google.maps.LatLng( '.json_encode($Lat).', '.json_encode($Long).' ) );

    }, 1000 );
}

</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=myapikey&callback=initMap">
</script>

The question I am hearing:我听到的问题:

How can I asynchronously load google map position based on changing coordinates provided by a php script?如何根据 php 脚本提供的不断变化的坐标异步加载谷歌地图位置?

Proposed solution:建议的解决方案:

I think the best way to accomplish this is to create a separate php script that will provide updated coordinates via json format.我认为实现此目的的最佳方法是创建一个单独的 php 脚本,该脚本将通过 json 格式提供更新的坐标。 Then consume the coordinates from your JavaScript in a separate php script with a fetch.然后在单独的 php 脚本中使用来自 JavaScript 的坐标进行获取。 You can run this inside of your setInterval function.您可以在 setInterval 函数中运行它。

Your new php script would end with something like this:您的新 PHP 脚本将以如下内容结尾:

echo json_encode(array('latitude' => $Lat, 'longitude' => $Lon));

Then inside of your setInterval you could do something like this:然后在你的 setInterval 里面你可以做这样的事情:

setInterval( function(){ 
  fetch(`newphpscript.php`)
    .then((response) => response.json())
    .then((data) => {
      marker.setPosition(new google.maps.LatLng(data.latitude, data.longitude));
      map.panTo( new google.maps.LatLng(data.latitude, data.longitude ));
  });
}, 1000 );

Please comment if you need any further clarification.如果您需要任何进一步的说明,请发表评论。

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

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