1

我在 C# Web 表单项目中使用最新版本的 GoogleMaps.Subgurim.NET dll。我想知道如何使用该组件在 v3 中获得 Google Maps v2 的 clearOverlays() 方法的效果。让我给你举个例子; 在地图单击事件上,我曾经删除所有现有标记,然后使用 InfoWindow 放置一个新标记:

protected string GMap1_Click(object s, GAjaxServerEventArgs e)
        {

            GMarker marker = new GMarker(e.point);
            string strInfoWindow = string.Format(
                                             @"<b>Mytext<br> "lat = {0}<br/>lng = {1}",
                                             e.point.lat,
                                             e.point.lng);
            GInfoWindow window = new GInfoWindow(marker,
                                                 strInfoWindow,
                                                 true);

            return 
                e.map+".clearOverlays();"+
                window.ToString(e.map);
}

代码中唯一需要注意的部分是最后三行。您能否告诉我如何在这种情况下更改我的 javascript 代码以删除所有标记?

预先感谢您的任何帮助。新康萨斯

4

1 回答 1

1

I solved the problem. I simply push the markers created via Subgurim component into an array and then call a custom js function attached to the map control that deletes them. Here is the code for attaching the function:

       StringBuilder sb = new StringBuilder();

       sb.Append("var markersArray=[];");
       sb.Append("function clearOverlays() {");
       sb.Append("   for (var i = 0; i < markersArray.length; i++ ) {");
       sb.Append("     markersArray[i].setMap(null);");
       sb.Append("   }");
       sb.Append("   markersArray = [];");
       sb.Append("}");

       GMap1.Add(sb.ToString());

And this is how my Gmap1_Click event looks like now:

protected string GMap1_Click(object s, GAjaxServerEventArgs e)
    {
        GMarker marker = new GMarker(e.point);
        string strInfoWindow = string.Format(
                                         @"point<br />lat = {0}<br/>lng = {1}",
                                         e.point.lat,
                                         e.point.lng);
        GInfoWindow window = new GInfoWindow(marker,
                                             strInfoWindow,
                                             true);
        return
               "clearOverlays();" +
               window.ToString(e.map)+
               "markersArray.push(" + GMap1.getGMapElementById(marker.ID) + ");";
    }

Everything works fine in this way.

于 2013-10-10T08:12:02.060 回答