提交表单后谷歌地图刷新

时间:2013-07-04 00:51:38

标签: php ajax forms google-maps

我希望在提交表单后重新初始化我的地图。我的表单有一个下拉菜单,下拉列表用于选择将在地图中显示的区域。我想在表单提交时,页面仍在那里,但地图刷新并显示标记。对不起,我的英语不好。有代码:

<script type="text/javascript">
                function initialize(){
                var peta;
                var gambar_tanda;
                gambar_tanda = 'assets/images/enseval.jpg';
                var x = new Array();
                var y = new Array();
                var customer_name = new Array();
                    // posisi default peta saat diload
                    var lokasibaru = new google.maps.LatLng(-6,107);
                    var petaoption = {
                        zoom: 6,
                        center: lokasibaru,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
                    var infowindow =  new google.maps.InfoWindow({
                                      content: '<div class=new>""</div>'
                    });
                    // memanggil function ambilpeta() untuk menampilkan koordinat
                    url = "json.php";
                    $.ajax({
                        url: url,
                        dataType: 'json',
                        cache: false,
                        success: function(msg){
                            for(i=0;i<msg.enseval.customer.length;i++){
                                x[i] = msg.enseval.customer[i].x;
                                y[i] = msg.enseval.customer[i].y;
                                customer_name[i] = msg.enseval.customer[i].nama_customer;
                                var point = new google.maps.LatLng(parseFloat(msg.enseval.customer[i].x),parseFloat(msg.enseval.customer[i].y));
                                  tanda = new google.maps.Marker({
                                            position: point,
                                            map: peta,
                                            icon: gambar_tanda,
                                            clickable: true
                                });
                            bindInfoWindow(tanda, peta, infowindow, msg.enseval.customer[i].nama_customer);
                            }
                        }
                    });
                }

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

                function bindInfoWindow(tanda, peta, infowindow, data) {
                    google.maps.event.addListener(tanda, 'click', function() {
                    infowindow.setContent(data);
                    infowindow.open(peta, tanda);
                    });
                }
                function reload(form){
                var val=form.org_id.options[form.org_id.options.selectedIndex].value;
                self.location='index.php?cabang=' + val ;
                }
                </script> 
                </head>
                <body>
                <div id="map_canvas" style=" align: left; width:1000px; height:500px"></div>
                <?php require ('config.php'); 
                @$cabang=$_GET['cabang'];
                /*if(strlen($cabang) > 0 and !is_numeric($cabang)){ 
                echo "Data Error";
                exit;
                }*/
                $quer2=pg_query("SELECT DISTINCT org_id FROM gis order by org_id");
                if(isset($cabang) and strlen($cabang)){
                $quer=pg_query("SELECT DISTINCT cp_rayon_name FROM gis where org_id=$cabang order by cp_rayon_name"); 
                }
                //else{$quer=pg_query("SELECT DISTINCT cp_rayon_name FROM gis order by cp_rayon_name"); }
                echo "<form method=post name=f1 action='' onSubmit='\"initialize()\'>";
                echo "<select name='org_id' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
                while($noticia2 = pg_fetch_array($quer2)) { 
                if($noticia2['org_id']==@$cabang){echo "<option selected value='$noticia2[org_id]'>$noticia2[org_id]</option>"."<BR>";}
                else{echo  "<option value='$noticia2[org_id]'>$noticia2[org_id]</option>";}
                }
                echo "</select>";
                echo "<select name='rayon'><option value=''>Select one</option>";
                while($noticia = pg_fetch_array($quer)) { 
                echo  "<option value='$noticia[cp_rayon_name]'>$noticia[cp_rayon_name]</option>";
                }
                echo "</select>";
                echo "<input type=submit value=Submit>";
                echo "</form>";
                include ('json.php');
                ?>

                </body>
                </html>

 <?php
        require ('config.php');
        $rayon = $_POST['rayon'];
        $cabang = $_POST['org_id'];
        //echo "$rayon, $cabang";
        $sql = "SELECT distinct org_id, customer_name, attribute16, attribute17 FROM gis where cp_rayon_name = '$RAYON'";
        $data = pg_query($sql);
        $json = '{"enseval": {';
        $json .= '"customer":[ ';
        while($x = pg_fetch_array($data)){
            $json .= '{';
            $json .= '"id_customer":"'.$x['org_id'].'",
                "nama_customer":"'.htmlspecialchars($x['customer_name']).'",
                "x":"'.$x['attribute17'].'",
                "y":"'.$x['attribute16'].'"
            },';
        }
        $json = substr($json,0,strlen($json)-1);
        $json .= ']';
        $json .= '}}';
        //echo $json;
        ?>

1 个答案:

答案 0 :(得分:1)

首先,我无法理解你为什么在文件末尾包含json.php,也不能用于ajax,因为你评论了最后一行。我认为您无需使用self.location='index.php?cabang=' + val;重新加载此页面。只需使用json.php(您已在代码中使用)和使用$.ajax方法的成功更新地图(here

panTo()发送请求

所以算法应该是:<select> onchange - &gt; reload - &gt; val(您已经拥有它) - &gt; $.ajax - &gt; json.php + data = val - &gt; success: function(data) {} - &gt; var newCoor = new google.maps.LatLng(data.x, data.y) - &GT; Map.panTo(panTo(newCoor))