none
[求助] VB2008使用Google Maps? RRS feed

  • 問題

  • 目前案子須在VB上使用Google Maps, 在網路上搜尋相關資料, 目前在VB上點擊Map會回應到VB的Map_Click函式, 現在須要加入兩個動作:

    1. 點擊Marker, 將Marker的座標回應到VB, 以使VB知道是哪個Marker產生動作

    2. 能隨時改變Marker的Icon(圖示)

    但是程式加入後沒反應, 請問我要怎麼修改?

    謝謝回覆!!

    <<粗體字是我加入的部份>>

    --------------------------------------------------------------------------------------------------

    VB程式 :
    'Using the control
    Public Class Form1
        Private GoogleControl1 As GoogleControl
        Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            GoogleControl1 = New GoogleControl
            GoogleControl1.Dock = DockStyle.Fill
            Me.Controls.Add(GoogleControl1)
        End Sub
    End Class
    'The Control
    'The class needs to be marked as com visible so the
    'script in the GoogleMap.htm can call some of the subs.
    <System.Runtime.InteropServices.ComVisibleAttribute(True)> _
    Public Class GoogleControl : Inherits UserControl
        Private WebBrowser1 As WebBrowser
        Private StatusStrip1 As StatusStrip
        Private StatusButtonDelete As ToolStripButton
        Private StatusLabelLatLng As ToolStripStatusLabel
        Private InitialZoom As Integer
        Private InitialLatitude As Double
        Private InitialLongitude As Double
        Private InitialMapType As GoogleMapType
        'I use this enum to store the current map
        'type into the application's settings.
        'So when the user closes the map on Satellite
        'mode it will be on Satellite mode the next
        'time they open it.
        Public Enum GoogleMapType
            None
            RoadMap
            Terrain
            Hybrid
            Satellite
        End Enum
        Sub New()
            .....(略)
            'The default map settings.
            InitialZoom = 8
            InitialLatitude = 24.217634 '38
            InitialLongitude = 120.701603 '-96.5
            InitialMapType = GoogleMapType.RoadMap
        End Sub
        Sub New(ByVal zoom As Integer, ByVal lat As Double, ByVal lng As Double, ByVal mapType As GoogleMapType)
            'This constructor could be used to start the map with different values
            'other than the default settings.
            Me.New()
            InitialZoom = zoom
            InitialLatitude = lat
            InitialLongitude = lng
            InitialMapType = mapType
        End Sub
        Private Sub GoogleControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'Load the htm doc into the webrowser.
            'When it completes, intialize the map.
            WebBrowser1.DocumentText = My.Computer.FileSystem.ReadAllText("GoogleMap.htm")
        End Sub
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
            'Initialize the google map with the initial settings.
            'The Initialize script function takes four parameters.
            'zoom, lat, lng, maptype.  Call the script passing the
            'parameters in.
            WebBrowser1.Document.InvokeScript("Initialize", New Object() {InitialZoom, InitialLatitude, InitialLongitude, CInt(InitialMapType)})
            WebBrowser1.Document.InvokeScript("AddMarker", New Object() {"Test1", 24.217634, 120.701603,"icon1.jpg"})
            WebBrowser1.Document.InvokeScript("AddMarker", New Object() {"Test2", 24.207634, 120.711603,"icon2.jpg"})
        End Sub
        Public Sub Map_MouseMove(ByVal lat As Double, ByVal lng As Double)
            'Called from the GoogleMap.htm script when ever the mouse is moved.
            StatusLabelLatLng.Text = "lat/lng: " & CStr(Math.Round(lat, 4)) & " , " & CStr(Math.Round(lng, 4))
        End Sub
        Public Sub Map_Click(ByVal lat As Double, ByVal lng As Double)

        ......    

        End Sub
        Public Sub Map_Idle()
            'Would be a good place to load your own custom markers
            'from data source
        End Sub
        Public Sub Marker_Click(ByVal lat As Double, ByVal lng As Double)
            StatusLabelLatLng.Text = "Marker Click"
        End Sub

    End Class

    --------------------------------------------------------------------------------------------------

    GoogleMap.htm :

    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            html
            {
                height: 100%;
            }
            body
            {
                height: 100%;
                margin: 0;
                padding: 0;
            }
            #map_canvas
            {
                height: 100%;
            }
        </style>

        <script type="text/javascript" src="https://maps-api-ssl.google.com/maps/api/js?v=3&sensor=false"> </script>

        <script type="text/javascript"> 
       
            var map;
            var marker;
            var Markers = [];
        
            function Initialize(zoomLevel,lat,lng,type){ 
                //Get the type of map to start.
                //Need to convert the GoogleMapType enum
                //to an actual Google Map Type
                var MapType;
                switch (type)
                {
                    case 1:
                        MapType = google.maps.MapTypeId.ROADMAP;
                        break;
                    case 2:
                        MapType = google.maps.MapTypeId.TERRAIN;
                        break;
                    case 3:
                        MapType = google.maps.MapTypeId.HYBRID;
                        break;
                    case 4:
                        MapType = google.maps.MapTypeId.SATELLITE;
                        break;
                    default:
                       MapType = google.maps.MapTypeId.ROADMAP;
                };
           
                //Create an instance of the map with the lat, lng, zoom, and
                //type passed in
                var myLatlng = new google.maps.LatLng(lat,lng);  
                var myOptions = {zoom: zoomLevel,center: myLatlng,mapTypeId: MapType};
                var MarkerSize = new google.maps.Size(48,48);
                                 
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);    
                google.maps.event.addListener(map, 'click', Map_Click);
                google.maps.event.addListener(map, 'mousemove', Map_MouseMove);
                google.maps.event.addListener(map, 'idle',Map_Idle);
              
                marker = new google.maps.Marker({
                .getCenter(),
                map: map,
                title: ''
                });
                google.maps.event.addListener(marker, 'click', Marker_Click);
            } 
     
            function Map_Click(e){
                window.external.Map_Click(e.latLng.lat(),e.latLng.lng());
            }
           
            function Map_MouseMove(e){
                window.external.Map_MouseMove(e.latLng.lat(),e.latLng.lng());
            }
                    
            function Map_Idle(){
                window.external.Map_Idle();
            }
                   
            function DeleteMarkers()
            {
                if (Markers){
                    for (i in Markers){
                        Markers[i].setMap(null);
                        google.maps.event.clearInstanceListeners(Markers[i]);
                        Markers[i] = null;
                    }
                    Markers.length = 0;
                }
            }
            
            function AddMarker(name,lat,lng,image)
            {
                var MarkerLatLng = new google.maps.LatLng(lat, lng);    
                var MarkerOption = {map:map,,title:name,icon:image};
                var Marker = new google.maps.Marker(MarkerOption);
                Markers.push(Marker);
                MarkerLatLng = null;
                MarkerOption = null;
            }
           
            function Marker_Click(e){
                window.external.Marker_Click(e.latLng.lat(),e.latLng.lng());
            }

        </script>
       
    </head>
    <body>
        <div id="map_canvas" style="width: 100%; height: 100%">
        </div>
    </body>
    </html>

    2014年6月11日 上午 02:38

解答