-1

Hi i have an web app where the result that php get from mysql db includes coordinates for google maps api. Following are my code on php

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

the result array are as follow:

[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 and y value are used to map maps on google map api. In view template the google map api javascripts are as follow:

<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>

Now the issues are, how can i pass array of result from php to javascripts so that it can iterate through the x, y and business name and map out maps ?

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は末尾のコンマを許可していません)

たとえば、ハードコードされた 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 に答える