dsgs8208 2013-07-04 00:51
浏览 74
已采纳

提交表单后谷歌地图刷新

i want to make my map re-initialize after submit a form. my form has a drop down menu, the drop down is use to choose a region that will be shown in map. i want to when form is submit, the page is still in there, but the map refresh and shown the mark. sorry for my bad english. there is the code :

<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条回答 默认 最新

  • doushang8512 2013-07-04 02:12
    关注

    First of all, I can't understand why are you including json.php at the end of your file, also it won't work for ajax, cause you you commented last line. I think you don't need to reload this page using self.location='index.php?cabang=' + val;. Just send a request to json.php using $.ajax (you already have it in your code) and on success update map with panTo() method (description here)

    so the algorithm should be: <select> onchange -> reload -> val (you already have it) -> $.ajax -> json.php + data = val -> success: function(data) {} -> var newCoor = new google.maps.LatLng(data.x, data.y)->Map.panTo(panTo(newCoor))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用