2012-03-05 23 views
4

我目前使用Google Map版本2(對不起,使用舊版本,請不要建議我改用V3,因爲這是我們的要求)。基本上,該頁面應允許用戶添加地點並選擇該地點的類型(例如餐廳,酒吧,學校等)。這些信息將被添加到數據庫中,並將用於切換。添加地點現在正在工作,需要重定向到另一個PHP頁面(testmap.php),以便它將在地圖上顯示標記。但切換需要來自下拉列表中選定類型的信息。我的問題是,我不知道如何「將」類型與每個標記「關聯」。我不知道如何從「類型」下拉列表中傳輸數據。在Google Maps V2中對JavaScript使用PHP變量

P.S.

  1. 第四行到最後一行是地址數據在我的addNewMarker函數onSubmit中傳輸的代碼。

  2. 上面的PHP代碼是下拉列表,其中包含上述「類型」的可能值。

  3. 最後,我需要實際的代碼來了解如何從下拉列表中獲取標記類型,並與addNewMarker函數一起傳輸,以便testmap.php可以使用數據庫中的值並可以輕鬆切換。

非常感謝你的幫助我的人!

<script type="text/javascript"> 

      if(GBrowserIsCompatible()) { 
       map_compatible = true; 
      } 

      /* Initialize the map this will be called when the document is loaded from: <body onLoad="initialize_map();"> */ 

      function initialize_map() { 
       if(map_compatible) { 
       }  

      function addNewMarker(newAddress){ 
       var set; 
       var lat; 
       var longi; 
       if(set==null){ 
        set=1; 
        geocoder = new GClientGeocoder(); 
        geocoder.getLatLng(newAddress, function(point){ 
         if(!point){ 
          alert(address + " not found"); 
         }else{ 
          lat = point.y; 
          longi = point.x; 
          alert("The latitude of " + newAddress + " is " + lat + " and longitude is " + longi); 
          default_address.push([latitude,longitude]); 
          location.href="testmap.php?lat="+lat+"&longi="+longi+"&set="+set+"&newAdd="+newAddress; 
         }             
        }); 
       } 
      } 

      function getType(type){ 
       //markertype = type; 
       document.write("Hello"); 
      } 


    </script> 



     </head> 

    <body onLoad="initialize_map();"> 

<div id="main-wrapper"> 
     <div id="main-padding"> 

    <div id="map_canvas"></div> 
      <form name="markertype_form" method = "POST" action = "addMarker.php" class="form"> 
       <?php 
        @$submit = $_POST['view']; 
        $selectedtype = ''; 
        if(isset($_GET)){ 
         $connect = mysql_connect("localhost","root","abc123"); 
         mysql_select_db("mapping"); 
         $query="SELECT id,typename FROM markertypes ORDER BY typename"; 

         $result = mysql_query ($query); 
         echo "<select name='types'>"; 
         $types = strip_tags(@$_POST['types']); 
         echo "<option disabled>---------------------Select---------------------</option>"; 
         while($nt=mysql_fetch_array($result)){ 
          $selected = false; 
          // check if the current value equals the value submited 
          if($_POST['types'] == $nt['id']){ 
           $selected = true; 
           $selectedtype = $nt['typename']; 
          } 
          // show selected attribute only if $selected is true 
          echo "<option value='{$nt['id']}' ". ($selected ? "selected" : "") .">{$nt['typename']}</option>"; 
         } 
         echo '</select>'; 
         echo "</select>"; 
         echo "<input type='submit' name ='view' value='View Details'>";// Closing of list box 
         echo '<br>'; 
        } 
       ?> 

      </form> 
      <form name="direction_form" onSubmit="addNewMarker(this.new_address.value); return false;" class="form"> 

       Add markers? Enter the address and we will mark it for you: <input type="text" name="new_address" class="form-field" /><br /><br /> 

       <input type="submit" value=" Mark this place! " class="form-submit" /> 

      </form> 

回答

3

如果你在你的php的類型選擇框中添加一個id,那麼你可以在你的javascript上面添加一些代碼行。

下面是假設了「類型」選擇元件具有「標記類型」的ID:

function addNewMarker(newAddress){ 
      var set; 
      var lat; 
      var longi; 
      var e = document.getElementById("marker-type"); 
      var mtype = e.options[e.selectedIndex].value; 
      if(set==null){ 
       set=1; 
       geocoder = new GClientGeocoder(); 
       geocoder.getLatLng(newAddress, function(point){ 
        if(!point){ 
         alert(address + " not found"); 
        }else{ 
         lat = point.y; 
         longi = point.x; 
         alert("The latitude of " + newAddress + " is " + lat + " and longitude is " + longi); 
         default_address.push([latitude,longitude]); 
         location.href="testmap.php?lat="+lat+"&longi="+longi+"&set="+set+"&newAdd="+newAddress+"&type="+mtype; 
        }             
       }); 
      } 
     } 

請注意上面,即在AddMarker函數I增加了一個新的變量「MTYPE」,其選擇選擇框並獲取當前選項值。

然後下面,API調用testmap.php我添加了類型選擇器,並給它的值爲「mtype」var。

在testmap.php代碼之後,你只需按照使用越來越:

$_GET['type'] 
+0

非常感謝!工作!我不期待這個簡短但很好的答案!幫了很多! – 2012-03-05 08:36:11

+0

很高興爲您服務:) – 2012-03-05 08:45:02

相關問題