简体   繁体   English

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

[英]Adding a JavaScript function to a repeat region in PHP

I have an area on a webpage (defined as a div), displaying various bits of information about people from a database. 我在网页上有一个区域(定义为div),显示有关数据库中人员的各种信息。 I have a repeat region on the database to produce a new div for each user in the database. 我在数据库上有一个重复区域,为数据库中的每个用户生成一个新的div。

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. 我想要做的是在每个区域都有一张Google地图,显示用户从存储在数据库中的邮政编码的确切位置。 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. 另外,当我将包含地图<div id="map-canvas">的div的名称传递给google函数时,我认为不得不为每个区域创建一个唯一的ID,以便google函数知道哪个div它指的是。 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? 我如何实现这一目标,如何为每个重复区域区域添加“函数调用”,以便为每个区域创建一个新的映射?

You will certanly have to bind each map display area to the Google engine, but once it's done, you are free to change the display area without recreating the div. 您将必须将每个地图显示区域绑定到Google引擎,但一旦完成,您可以自由更改显示区域而无需重新创建div。

Moreover, I assume there will be a physical limit for the number of maps you can display on a single page. 此外,我假设您可以在单个页面上显示的地图数量存在物理限制。

So maybe you could create, say, half a dozen of pre-initialized map areas and use them to display the location(s) of whatever person(s) you're currently focusing in? 那么也许您可以创建六个预先初始化的地图区域,并使用它们来显示您当前关注的任何人的位置? It takes only a call to display the location from the address. 只需拨打电话即可显示地址中的位置。

Yes, you have to create a div to each(and with different id each) map. 是的,你必须为每个(并且每个具有不同的id )地图创建一个div 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 ): 创建一个具有地图公共属性的对象(我认为它们将具有一些常见属性,如zoommapTypeId ):

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

     $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. 当然这些代码就是一个例子,因为我不知道你的代码,所以你必须根据自己的需要进行调整。

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

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