页面上的WPF WebBrowser脚本无法正常工作

时间:2015-03-31 04:19:19

标签: c# wpf geolocation

当我在浏览器中运行html页面时,一切正常。但是在WPF WebBrowser中无法正常工作。元素没有值。

/// <summary>
/// Interaction logic for GeoDialog.xaml
/// </summary>
public partial class GeoDialog : Window
{
    public GeoDialog()
    {
        InitializeComponent();

        coord.LoadCompleted += webBrowser_LoadCompleted;
        coord.Loaded += delegate
        {
            coord.Navigate(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Location.html");
        };
        //GetPosition();
    }

    private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
            string latitude = "";
            string longitude = "";
            string error = "";

            var htmlDocument = coord.Document as HTMLDocument;
            var latitudeEl = htmlDocument.getElementById("latitude");
            var longitudeEl = htmlDocument.getElementById("longitude");
            var errorEl = htmlDocument.getElementById("error");

            latitude = latitudeEl.getAttribute("value");
            longitude = longitudeEl.getAttribute("value");
            error = errorEl.getAttribute("value");

            if (String.IsNullOrEmpty(error))
            {
                MessageBox.Show(String.Format("Latitude: {0} Longitude: {1}", latitude, longitude));
            }
            else
                MessageBox.Show(String.Format("Error: {0}", error));
    }
}

Location.html以下标记:

 <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <meta http-equiv="X-UA-Compatible" content="IE=10" />
        <script type="text/javascript">
            window.onload = function () {
                var latitude = document.getElementById("latitude");
                var longitude = document.getElementById("longitude");
                var error = document.getElementById("error");

                function getLocation() {
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(success, error, options);
                    }
                }

                var options = {
                    enableHighAccuracy: true,
                    timeout: 5000,
                    maximumAge: 0
                };

                function success(position) {
                    latitude.value = position.coords.latitude;
                    longitude.value = position.coords.longitude;
                };

                function error(err) {
                    error.value = 'ERROR(' + err.code + '): ' + err.message;
                };


                function showPosition(position) {
                    latitude.value = position.coords.latitude;
                    longitude.value = position.coords.longitude;
                }
                getLocation();
            }
        </script>
    </head>
    <body>
        <input type="hidden" id="latitude" />
        <input type="hidden" id="longitude" />
        <input type="hidden" id="error" />
    </body>
    </html>

也许还有其他选项来确定地理坐标。

1 个答案:

答案 0 :(得分:0)

我使用了这段代码,现在一切正常!

internal GeoLoc GetMyGeoLocation()
        {
            try
            {
                var address = ".........";
                var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Uri.EscapeDataString(address));
                request = WebRequest.Create(requestUri) as HttpWebRequest;
                if (request != null)
                {
                    var response = request.GetResponse();
                    var xdoc = XDocument.Load(response.GetResponseStream());
                    var result = xdoc.Element("GeocodeResponse").Elements("result");
                    foreach (var item in result)
                    {
                        _geoLoc = new GeoLoc();
                        _geoLoc.Country = item.Element("formatted_address").Value;
                        var locationElement = item.Element("geometry").Element("location");
                        _geoLoc.Latitude = locationElement.Element("lat").Value;
                        _geoLoc.Lognitude = locationElement.Element("lng").Value;
                    }
                }
                return _geoLoc;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return new GeoLoc();
        }