dongxie2749 2013-08-13 21:55
浏览 29
已采纳

Google Maps AP v3 I - 尝试使用jQuery $ .ajax动态添加标记

I am using the Google Maps API v3 to create a map for my application to show viewers on our Webcast. The idea is to add a new marker on the map when a new viewer jumps onto the stream. That is the map is not reloaded, only a new marker is added.

I am trying to accomplish this with an ajax call to a script that gets the current viewers. I have an output to the console to show the results, and the latitude and longitude coordinates are correct, so I don't know why it does not put the marker onto the map.

I am referencing the same class in the page with the map as I am with the ajax script call, so I can do a PHP foreach loop in the middle of the javascript on the page and it puts the marker onto the map! But, of course, I need to use ajax to get new serverside info (new viewers).

js with php foreach() (works)

note: this is how I tested it, it is in $.ajax success function, but has nothing to do with the ajax script

//add markers
function addMarker(stream_id) {
  $.ajax({
    dataType: 'json',
    url: '../../stream/' + stream_id + '/mapmarkers',
    success: function(result) {
      <?php foreach($stream_viewer_info as $viewer) : ?>

        markers.push(new google.maps.Marker({
          position: new google.maps.LatLng(<?= $viewer['location_info']['latitude'] . ', ' . $viewer['location_info']['longitude']; ?>),
          map: map
        }));

      <?php endforeach; ?>

    },
    complete: function() {
      setTimeout(function(){addMarker(<?=$stream_id; ?>)}, 1000);
    }


  });
}

js with js for() (does not work)

//add markers
function addMarker(stream_id) {
  $.ajax({
    dataType: 'json',
    url: '../../stream/' + stream_id + '/mapmarkers',
    success: function(result) {
      for(var i = 0; i < result.length; i++) {
          console.log('result ' + i + ': ' + result[i]['latitude'] + ', ' + result[i]['longitude']);

        markers.push(new google.maps.Marker({
          position: new google.maps.LatLng(result[i]['latitude'] + ', ' + result[i]['longitude']),
          map: map
        }));

      }

    },
    complete: function() {
      setTimeout(function(){addMarker(<?=$stream_id; ?>)}, 1000);
    }


  });
}
  • 写回答

1条回答 默认 最新

  • douzhulv1699 2013-08-13 22:11
    关注

    Try this

    markers.push(new google.maps.Marker({
        position: new google.maps.LatLng(result[i]['latitude'], result[i]['longitude']),
        map: map
    }));
    

    You need to separate the arguments by an actual comma, not a string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)