-1

嗨,我有一个网络应用程序,其中 php 从 mysql db 获得的结果包括谷歌地图 api 的坐标。以下是我在 php 上的代码

$businessMapper = new Application_Model_Mapper_BusinessMapper();
$biz = $businessMapper->searchBusinessByCityAndName($searchtext,$city);
$this->view->biz = $biz;

结果数组如下:

[0] => Array
                    (
                        [business_name] => Madam Kwan's Restaurant KLCC
                        [business_url] => Madam-Kwan-s-Restaurant-KLCC
                        [reviews_num] => 2
                        [business_id] => 134
                        [rating] => 3
                        [business_phone] => 03-2026 2297
                        [business_add1] => Suria Klcc 
                        [business_add2] => Jalan Ampang
                        [x] => 101.74357729999997
                        [y] => 3.1597723
                        [photo_url] => 201302051423582
                        [cat_name] => Restaurants
                    )

                [1] => Array
                    (
                        [business_name] => Spring Garden Restaurant
                        [business_url] => Spring-Garden-Restaurant
                        [reviews_num] => 2
                        [business_id] => 135
                        [rating] => 4
                        [business_phone] => 03-2166 9881
                        [business_add1] => Lot 413-414, Level 4
                        [business_add2] => Kuala Lumpur City Centre
                        [x] => 101.71465799999999
                        [y] => 3.1567893
                        [photo_url] => 201302011217282
                        [cat_name] => Restaurants
                    )

                [2] => Array
                    (
                        [business_name] => Hard Rock's Cafe Kuala Lumpur
                        [business_url] => Hard-Rock-s-Cafe-Kuala-Lumpur
                        [reviews_num] => 1
                        [business_id] => 137
                        [rating] => 2
                        [business_phone] => 03-2715 5555
                        [business_add1] => Ground Floor, Wisma Concorde
                        [business_add2] => Jalan Sultan Ismail
                        [x] => 101.70525199999997
                        [y] => 3.155552
                        [photo_url] => 201302011203172
                        [cat_name] => Restaurants
                    )

                [3] => Array
                    (
                        [business_name] => Sao Nam
                        [business_url] => Sao-Nam
                        [reviews_num] => 1
                        [business_id] => 141
                        [rating] => 4
                        [business_phone] => 03-2144 1225
                        [business_add1] => Anggun Boutique Hotel 7 & 9
                        [business_add2] => Tengkat Tong Shin
                        [x] => 101.70798279999997
                        [y] => 3.1463384
                        [photo_url] => 201302011122322
                        [cat_name] => Restaurants
                    )

                [4] => Array
                    (
                        [business_name] => Palate Palette
                        [business_url] => Palate-Palette
                        [reviews_num] => 1
                        [business_id] => 150
                        [rating] => 3
                        [business_phone] => 03-2142 2148
                        [business_add1] => 21 Jalan Mesui
                        [business_add2] => 
                        [x] => 101.70791299999996
                        [y] => 3.149042
                        [photo_url] => 201302011031132
                        [cat_name] => Restaurants
                    )

x 和 y 值用于在 google map api 上映射地图。在视图模板中,谷歌地图 api javascripts 如下:

<script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
 <script type="text/javascript">


    var locations = [
     {business_name1},{x1},{y1}
     {business_name2},{x2},{y2}
     etc...
    ];

    var map = new google.maps.Map(document.getElementById('map-container'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>

现在的问题是,我如何将结果数组从 php 传递到 javascripts,以便它可以遍历 x、y 和企业名称并绘制地图?

4

2 回答 2

1

我想粗略的方法是使用您的 JS 脚本标签将变量输出到正确的位置。

<script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
 <script type="text/javascript">

    var locations = [    
<?php

foreach($results as $biz){
     echo '{' . $biz['business_name'] . '},{ ' . $bix['x'] . '},{ ' . $biz['y'] . '}';
}
?> 

    ];

这将复制您在示例中显示的内容,但您可能需要注意 JS 如何处理数组(即 JS 不允许尾随逗号 AFAIK)

比如说,首先在页面上运行 2 个硬编码的 JS 结果,然后使用我展示的 echo 方法来复制真正起作用的方法。

如果您想要它的 JSON,您可以使用 PHP json_encode() 函数通过选择结果的子集或删除您不想要的结果(url、评级等)来输出

根据您的情况,您可以将结果数组或 json 存储在外部 .js 文件中,这样您也可以缓存结果。

于 2013-02-16T11:01:31.027 回答
0

用这个,

    var locations = [
    <?php
    foreach($resultarray as $row){
    echo "{".$row['business_name']."},{".$row['x']."},{".$row['y']."}";
    }
    ?>
    ];
于 2013-02-16T11:26:40.360 回答