I am currently working on a project that involves allowing users to input an address and then click a button. The entered address will be displayed on the WebBrowser control in a .net windows application. I am aware that JavaScript can be executed on the WebBrowser using WebBrowser1.DocumentText, followed by calling the script with WebBrowser1.Document.InvokeScript...
While I have encountered some challenges with this process, I am hopeful that someone here can guide me in the right direction.
CODE:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim AddressMap As String
AddressMap = AddressM.Text
WebBrowser1.DocumentText = "<html><head><meta name='viewport' content='initial-scale=1.0, user-scalable=no' />" & _
"<script type='text/javascript' src='http://maps.google.com.mx/maps/api/js?sensor=true&language;=es'></script>" & _
"<script type='text/javascript'>" & _
"var geocoder; var map;" & _
"function initialize() " & _
"{geocoder = new google.maps.Geocoder(); var myOptions = { zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP()" & _
"} var(address = " & AddressMap & ")" & _
"geocoder.geocode({ 'address': address }, function (results, status) {" & _
"if (status == google.maps.GeocoderStatus.OK) {" & _
"map.setCenter(results[0].geometry.location);" & _
"var marker = new google.maps.Marker({" & _
" map: map," & _
"position: results[0].geometry.location });" & _
"} else {" & _
"}});" & _
"map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);}" & _
"</script></head><div id='map_canvas' style='width:100%; height:100%'></div></body></html>"
WebBrowser1.Document.InvokeScript("Initialize")
End Sub
Here is another code snippet which I believe should work, but it still triggers a script error "An error has occurred in the script on this page" Line 1 Char 124 Error Expected ';' Code 0 URL about.blank
WebBrowser1.DocumentText = "<html><head><script type='text/javascript' src='http://maps.google.com.mx/maps/api/js?sensor=false&language;=es'></script> " +
"<script type='text/javascript'> " +
"var geocoder; " +
"var map; " +
"function initialize(address) { " +
"geocoder = new google.maps.Geocoder(); " +
"var myOptions = { zoom: 16 } " +
"geocoder.geocode({ 'address': address }, function (results, status) { map.setCenter(results[0].geometry.location); " +
"var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); " +
"map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); " +
"} " +
"</script> " +
"</head> " +
"<body> <div id='map_canvas' style='width:100%; height:100%'></div> </body> </html>"
WebBrowser1.Document.InvokeScript("initialize", New String() {AddressM.Text})
Appreciate any assistance provided.
Leo P.
This final piece of code is slightly different from the previous two and contains an if-else statement extracted from HTML that executes the Google Maps script.
WebBrowser1.DocumentText = "<html><head><script type='text/javascript' src='http://maps.google.com.mx/maps/api/js?sensor=false&language;=es'></script> " +
"<script type='text/javascript'> " +
"var geocoder; " +
"var map; " +
"function initialize() { " +
"geocoder = new google.maps.Geocoder(); " +
"var myOptions = { zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP } " +
"var(address = 'Miami Beach, Florida') " +
"geocoder.geocode({ 'address': address }, " +
"function (results, status) { " +
"if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); " +
"var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); " +
"} else { alert('Geocode was not successful !);}}); " +
"map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); } " +
"</script> " +
"</head> " +
"<body> <div id='map_canvas' style='width:100%; height:100%'></div> </body> </html>"
WebBrowser1.Document.InvokeScript("initialize")
Shown below is the HTML code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=true&language=es"></script>
... (remaining HTML code)...