2013-02-16 174 views
-1

嗨,我有一個Web應用程序,其中PHP從MySQL數據庫獲得的結果包括谷歌地圖API的座標。以下是我在PHP代碼PHP的谷歌地圖Api

$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的值被用來映射在谷歌地圖API地圖。在視圖模板的谷歌地圖API的JavaScript如下:

<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來的JavaScript,以便它可以通過X,Y和企業名稱和地圖迭代出地圖?

+0

你確定的位置排列是這樣的... – 2013-02-16 11:15:13

回答

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不允許一個結尾逗號據我所知)

獲取說,2硬編碼JS首先在頁面上工作的結果,然後使用我展示的回顯方法來複制實際工作的內容。

如果它的JSON你願意,你可能只是使用PHP json_encode()函數,通過選擇一個子集的結果,或者丟棄結果輸出你不想(URL,等級等)

根據根據您的情況,您可以將結果數組或json存儲在外部.js文件中,這樣您也可以緩存結果。

0

使用此,

var locations = [ 
    <?php 
    foreach($resultarray as $row){ 
    echo "{".$row['business_name']."},{".$row['x']."},{".$row['y']."}"; 
    } 
    ?> 
    ];