I am using "WebBroswer" in my wpf application to render Google map. So I am calling Pan(x, y) JavaScript method with some parameters form my c# code.
But I am getting this below error.
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
My Window2.xaml File:
<Window x:Class="Test.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
<WebBrowser Name="mapBrowser" Margin="50" />
<Button Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button>
</Grid>
</Window>
My Window2.xaml.cs File:
namespace Test
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri(@"pack://application:,,,/HTMLPage1.htm");
Stream source = Application.GetContentStream(uri).Stream;
mapBrowser.NavigateToStream(source);
this.mapBrowser.InvokeScript("Pan", x, y);
}
}
}
My HTML Page:
<html>
<head>
<title></title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var latlng;
var lat;
var lng;
var zoom;
function Init() {
lat = 40.632915;
lng = -8.68929;
zoom = 18
latlng = new google.maps.LatLng(lat, lng);
var options = {
zoom: 18,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
}
function Pan(x, y) {
try {
lat = x;
lng = y;
map.panTo(new google.maps.LatLng(lat, lng));
}
catch (ex) {
window.alert("Pan:Error");
}
}
</script>
</head>
<body onload="Init()">
<div id="map_canvas" style="width: 100%; height: 100%">
</div>
</body>
</html>
Yes, I got the problem. I try to call the JavaScript function without loading the HTML page. So I have first loaded the html page and then called the JavaScript function and its working for me.
So I have changed my code as below and it is working fine :-
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Uri uri = new Uri(@"pack://application:,,,/HTMLPage1.htm");
Stream source = Application.GetContentStream(uri).Stream;
mapBrowser.NavigateToStream(source);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
this.mapBrowser.InvokeScript("Pan", 19.006145, 72.818148);
}
catch
{
}
}
}
}
It's better to move the JavaScript invoke to Document loaded event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With