Help us translate this website and improve this translation and earn free licenses!
Anonymous user  |  Log in  |  Create Account

Custom Inside Javascript

We'll use "CustomInsideJavascript" to add our own Javascript.

CustomInsideJavascript serves to add Javascript which will be executed in the loading of a map. That is to say, the added CustomInsideJavascript could not be accessed by other events like, for example, the click of a button. For that we have CustomJavascript

To use CustomInsideJavascript, we will have to use the method "ToString()" to output elements of our control in Javascript for the Google API.

Our example is very simple, and we combined the CustomInsideJavascript with the CustomJavascript. By means of the CustomJavascript we created a function that will add icons in random locations, but because it cannot initiate the event, we used the CustomInsideJavascript call once (and only once) in the code.

As you can see, the methodology followed in the example supposes the typical use of the CustomInsideJavascript: to call to a CustomJavascript function at the moment the map loads.




Code.aspx
<cc1:GMap ID="GMap1" runat="server" />
<input type="button" id="Button1" value="Añadir icono" onclick="subgurim_Add()" />
<input type="button" id="Button2" value="Borrar último icono" onclick="subgurim_Delete()" />
<input type="button" id="Button3" value="Borrar todos los iconos" onclick="subgurim_Remove()" />
Code.aspx.cs
System.Text.StringBuilder sb = new System.Text.StringBuilder();

GLatLng center = new GLatLng(44, 5);
GMap1.setCenter(center, 8);

//GMap1.Add("addRandom();", true);
GMap1.addCustomInsideJavascript("addRandom();");

sb.Append("var max = 10;");
sb.Append("function addRandom()");
sb.Append("{");
GMarker marker = new GMarker(
string.Format("new GLatLng({0}.getCenter().lat() + Math.random() - 0.5, {0}.getCenter().lng() + Math.random() - 0.5)", GMap1.GMap_Id));
sb.Append(marker.ToString(GMap1.GMap_Id));
sb.Append("if (max-- > 0)");
sb.Append("setTimeout('addRandom()', 1000)");
sb.Append("}");

GMap1.Add(sb.ToString());
Powered by Subgurim.NET