2014-09-28 113 views
1

我在地圖上有多個標記。點擊事件只適用於一個標記,可能是地圖上「加載」的最後一個標記。它調用一個C#方法,它的工作原理,但只在最後一個標記。其他標記不會導致事件並調用該方法。我想知道有沒有人可以幫忙?下面是html(實際上是PHP)頁面的代碼,然後是C#代碼。我想我需要修改Goolge地圖代碼,但嘗試了很多事情,但都沒有運氣。Click Event Works Only Only Last Marker

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 

    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
    <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.9/src/markerwithlabel.js" type="text/javascript"></script> 

    <style type="text/css"> 
    html, body { padding: 0; margin: 0 } 
    .labels { 
     color: red; 
     background-color: white; 
     font-family: "Verdana", "Arial", sans-serif; 
     font-size: 10px; 
     font-weight: bold; 
     text-align: center; 
     width: 50px;  
     border: 1px solid black; 
     white-space: nowrap; 
    } 
    </style> 

</head> 
<body> 
    <?php 
     $con=mysqli_connect("localhost","root","","realestate_db"); 
     if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();} 
     $result = mysqli_query($con,"SELECT `ID`, `lat` , `long` FROM `house` "); 
    ?> 

    <div id="map" style="width: 500px; height: 400px;"></div> 

    <script type="text/javascript"> 
     var pt; 
     var map = new google.maps.Map(document.getElementById('map'), { 
      mapTypeId: google.maps.MapTypeId.ROADMAP }); 
     var markerBounds = new google.maps.LatLngBounds(); 

     <?php while($row = mysqli_fetch_array($result)) { ?> 
       pt = new google.maps.LatLng(<?php echo $row['lat'] . "," . $row['long']; ?>); 
       marker = new MarkerWithLabel({ 
        position: pt, 
        map: map, 
        title: 'House', 
        labelContent: "House " + <?php echo $row['ID'] ?> , 
        labelAnchor: new google.maps.Point(-10, 50), 
        labelClass: "labels", 
        labelStyle: {opacity: 0.75} 
       }); 
       markerBounds.extend(pt); 
     <?php } 

       mysqli_close($con); 

     ?> 

     map.fitBounds(markerBounds); 

     google.maps.event.addListener(marker, "click", function (e) { 
     window.external.GetApplicationName(); }); 



    </script> 
</body> 
</html> 

的C#:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace houseDB1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      webBrowser1.ObjectForScripting = new ExternalApplication(); 
     } 

     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      webBrowser1.Navigate("127.0.0.1/box3.php"); 
     } 

     private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 

     } 

     [ComVisible(true)] 
     public class ExternalApplication 
     { 
      public void GetApplicationName() 
      { 
       MessageBox.Show("ZZTOP"); 
       //return "The application"; 
      } 
     } 



    } 


} 

感謝。

回答

0

我懷疑這是因爲你只是增加了click聽衆到最後MarkerWithLabel對象參考變量。

嘗試移動下面的一段代碼while塊內,使聽者增加了在每個迭代中創建的每個MarkerWithLabel例如:

google.maps.event.addListener(marker, "click", function (e) { 
     window.external.GetApplicationName(); }); 
+0

是的,它的工作。現在我明白了它的完美意義。我甚至可以將參數/數據傳回給C#。這些事件和函數調用有一些很好的「內部簿記」。非常感謝! – user1325143 2014-09-28 06:14:42