单击仅适用于最后一个标记的事件

时间:2014-09-28 04:48:05

标签: c# google-maps google-maps-markers

我在地图上有多个标记。 click事件仅适用于一个标记,可能是地图上“加载”的最后一个标记。它调用C#方法,但它只能在最后一个标记上运行。其他标记不会导致事件并调用该方法。我想知道是否有人可以提供帮助?下面是html(实际是PHP)页面的代码,然后是C#代码。我想我需要修改Goolge地图代码,但尝试了许多没有运气的事情。

<!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";
            }
        }



    }


}

感谢。

1 个答案:

答案 0 :(得分:0)

我怀疑是因为您只将click侦听器添加到MarkerWithLabel变量引用的最后一个marker对象中。

尝试在while块中移动以下代码段,以便为每次迭代中创建的每个MarkerWithLabel实例添加侦听器:

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