doushen2154 2014-01-09 23:01
浏览 32
已采纳

在PHP中将重复区域添加JavaScript函数

I have an area on a webpage (defined as a div), displaying various bits of information about people from a database. I have a repeat region on the database to produce a new div for each user in the database.

What I would like to do is have a Google map in each of these areas showing the exact location of the user from the postcode stored in the database. Ive achieved this in the first area but only because I am calling the function:

google.maps.event.addDomListener(window, 'load', initialize);

and so am only calling the function once, when the page is loaded (correct?).

also, as I am passing the name of the div containing the map <div id="map-canvas"> to the google function, I presume ill have to create a unique ID for each of the areas so the google function knows which div its referring to. How do I accomplish this, and how do I include the 'function call' for each of the repeat region areas so as to create a new map for each one?

  • 写回答

2条回答 默认 最新

  • dqphg40600 2014-01-09 23:11
    关注

    Yes, you have to create a div to each(and with different id each) map. You can reuse some code like this:

    1. Create an object with common properties of the maps(I supose they will have some common properties like zoom or mapTypeId):

      var mapOptions = {
          zoom: 10,
          mapTypeId: google.maps.MapTypeId.TERRAIN 
      };
      
    2. Then your PHP loop:

      $i = 0;
      while (@extract(mysql_fetch_assoc($resource)))
      {
          // Javascript code
          ?>
          var newOptions = mapOptions;
          newOptions["center"] = new google.maps.LatLng(<?php echo "$lat, $lng"; ?>);
          var map<?php echo $i; ?> = new google.maps.Map(document.getElementById('map<?php echo $i; ?>'),
        newOptions);
          <?php
      
          // HTML elements
          <?php echo "<div id='map$i'></div>"; ?> 
          $i++;
      }
      

    Of course these codes are an example because I don't know your code, so you'll have to adjust to your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64