Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Initially Iam using Webbrowser in vb.net for googlemap. now google not supporting the Webbrowser.So Iam using geckoWebbrowser.

map is displaying in browser but my issue is when i click map the geolocation to pass to vb.net form function .but iam unable to call vb.net form function in javascript side.
initially we r using window.host in webbrowser.
please give me solution for that.

What I have tried:

javascript code
--------------------
function Initialize()
            {  
				var stZoom=10;
                var MapType=google.maps.MapTypeId.ROADMAP;
                var myLatlng = new google.maps.LatLng("13.12","76.12"); 
                var myOptions = {zoom: parseInt(stZoom),center: myLatlng,mapTypeId: MapType};
				map = new google.maps.Map(document.getElementById("map_canvasADD"), myOptions);  
				currentMarker = new google.maps.Marker({map: map,draggable: false,position: {lat: "13.12", lng: "76.12"},index:0});  
				currentMarker.setPosition(myLatlng);
                google.maps.event.addListener(map, 'click', MapGeo);
            }


function MapGeo(e){
                   
                    window.external.MapGeoDotnet(e.latLng.lat(),e.latLng.lng());
	
			}

----- End JavaScript----

-----vb.net Side -----

Public Sub MapGeoDotnet(ByVal lat As Double, ByVal lng As Double)
      Msgbox(lat & " " & lng)

  End Sub
Posted
Updated 13-Dec-22 23:24pm
v2

1 solution

Looks like you need to use a message event to trigger your VB.NET code from Javascript:
https://stackoverflow.com/questions/15710581/how-to-call-c-sharp-method-in-javascript-by-using-geckofx-as-the-wrapper-of-xulr[^]
Window.postMessage() - Web APIs | MDN[^]
JavaScript
function MapGeo(e){
    const event = new MessageEvent("MapGeo", { 
        view: window, 
        bubbles: true, 
        cancelable: false, 
        data: JSON.stringify({ lat: e.latLng.lat(), lng: e.latLng.lng() })
    });
    
    document.dispatchEvent(event);
}
VB.NET
browser.AddMessageEventListener("MapGeo", data => 
{
    Dim o As JObject = JObject.Parse(data)
    Dim lat As Double = CDbl(o.SelectToken("lat"))
    Dim lng As Double = CDbl(o.SelectToken("lng"))
    MapGeoDotnet(lat, lng)
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900