从数据库动态填充谷歌地图位置


dynamically populate google maps location from db

我在 db 中有特定用户的纬度和经度.. 我使用 jquery 获取值.. 这就是我正在做的事情

var auto_refresh = setInterval(
        function ()
        {
            $.get('http://localhost/Location/locate', function(data){
             $('.latitude').html(data.lat);
             $('.longitude').html(data.long);

             }, 'json');

        }, 1000);

当我从PHP获取位置时,我正在这样做

    'localize' => false,
    'latitude' => $latitude,
    'longitude' =>  $longitude,
    'address' => $address,
    'marker' => true,

现在我正在这样做,但给了我语法错误

      'latitude' => ?><div class = 'latitude'></div><?php ,
    'longitude' => ?><div class = 'longitude'></div><?php ,

我想问的另一件事是,这是无需页面刷新即可获取位置的最佳方法..或者还有其他东西。

代码中关于 PHP 的一般问题和使用 https://github.com/marcferna/CakePHP-GoogleMapHelper:

  1. 您正在将<div class = 'latitude'></div><div class = 'longitude'></div>分配给纬度和经度,当它期望只是一个数字,所以这永远不会奏效。

  2. PHP
  3. 是服务器端,Javascript 在客户端执行,因此赋值永远不会起作用,因为 PHP 已经执行了。查看更多有关此问题的问题:如何将 javascript 变量值分配给 php 变量

溶液

使用您提供的代码...解决方案可能是直接使用Google Maps JavaScript API使用来自服务器的位置更新地图:setCenter(latlng:LatLng)

var auto_refresh = setInterval(function() {
  $.get('http://localhost/Location/locate', function(data){
    # Get the map instance and modify the center
    # map_canvas is the default variable name that contains the Google Map
    map_canvas.setCenter(new google.maps.LatLng(data.lat, data.long));
  }, 'json');
}, 1000);
我认为这

在 PHP 中是不允许的,在数组赋值中内联关闭和打开 PHP。 试试这个:

'latitude' => '<div class ="latitude"></div>',
'longitude' => '<div class ="longitude"></div>',