从db动态填充谷歌地图位置

时间:2013-09-22 13:55:19

标签: php ajax json google-maps jquery

我在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 ,

另一件我想问的是,这是获取没有页面刷新的位置的最佳方法..或者还有别的东西。

2 个答案:

答案 0 :(得分:0)

我不认为在PHP中允许这样做,在数组赋值中关闭并打开PHP内联。试试这个:

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

答案 1 :(得分:0)

您的代码中有一些关于PHP的问题以及关于使用https://github.com/marcferna/CakePHP-GoogleMapHelper

的问题
  1. 您正在为经纬度分配<div class = 'latitude'></div><div class = 'longitude'></div> 期待只有一个号码才能永远不会起作用。

  2. PHP是服务器端,Javascript在客户端执行,因此分配永远不会起作用,因为PHP已经被执行了。查看有关此问题的更多问题:How to assign the javascript variable value to php variable

  3. <强>解决方案

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

    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);
    
相关问题