1

我有一個包含google maps api v3腳本的map.html文件,我一直在嘗試使用webbrowser1.DocumentText和Webbrowerser.Document.InvokeScript來運行此腳本不成功。在.net窗口中修改html文件應用

這次我把map.html託管在一個網站上,我的目標是能夠修改這個html文件,然後在我的windows應用程序上運行它以顯示所需的地址。

下面

是託管前的的map.html代碼:http://url.com/map.html

<!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> 
<script type="text/javascript"> 

    var geocoder; 
    var map; 


    function initialize() { 

     geocoder = new google.maps.Geocoder(); 
     //var latlng = new google.maps.LatLng(-34.397, 150.644); 
     var myOptions = { 
      zoom: 16, 
      //center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 

     //var address = document.getElementById("address").value; 
     var address = "Miami Beach, Flordia" //Address to modify in order to display 

     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 for the following reason: " + status); 
      } 
     }); 

     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    } 


    </script> 
    </head> 
    <body onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:100%"></div> 

    </body> 
</html> 

如果您複製並粘貼到一個HTML應該顯示邁阿密海灘這段代碼,FL

現在

我Windows應用程序我想編輯這個網站上託管的HTML我想改變佛羅里達州邁阿密海灘到佛羅里達州那不勒斯爲例。

然後用我的Windows應用程序,一個網頁瀏覽器,並顯示爲WebBrowser1.Navigate時(「http://url.com/map.html」)

你的幫助是非常讚賞的。

我發現如何修改html,當它保存在本地計算機上,但對於我確切需要的這不是一種可行的方法。

謝謝你,

利奧P.

回答

0

我不會試圖修改HTML代碼。由於Google地圖代碼都是JS,我會編寫一個JS函數將地圖移動到新位置。 你可以從你的應用程序調用該函數(或者甚至可以從那裏插入)。

using mshtml; 

//First, navigate to your page: 
Webbrowser1.Navigate("http://url.com/map.html") 


void Webbrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
     //Then call your move function with the new target: 
     mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)Webbrowser1.Document.DomDocument; 
     mshtml.IHTMLWindow2 window = (mshtml.IHTMLWindow2)doc.parentWindow; 
     window.execScript("yourMapMoveFunction('Naples,Florida');"); 
} 

順便說一句,你的鏈接不顯示在地圖......

+0

有人幫我解決這個問題的另一篇文章,其中我其實是試圖運行網頁瀏覽器的整個腳本。他的解決方案也適用於這個問題。 「http://stackoverflow.com/questions/11705729/net-windows-application-webbrowser-google-maps-api-v3」非常感謝你的幫助! – TopoX 2012-08-04 18:37:52

相關問題