Обновление положения маркера Google maps
Привет
Я пытаюсь переместить существующую позицию маркера через VB.NET. Нет никаких ошибок в компиляторе или html, и ничего не происходит вообще. Существующий маркер остается там, где он есть.
Я храню данные маркера в словаре (String, MarkerData), где строка является идентификатором ("Hi" в этом примере), а MarkerData содержит широту, долготу и путь значка для маркера.
Imports System.IO Public Class GoogleMap Private window As WebBrowser Private markers As New Dictionary(Of String, MarkerData) Public Sub New(ByRef wb As WebBrowser) window = wb End Sub Public Sub registerMarker(id As String, icon As String) markers.Add(id, New MarkerData(icon)) End Sub Public Sub updateMarker(id As String, lat As Double, lng As Double) markers(id) = New MarkerData(lat, lng, markers(id).getIcon()) window.Document.InvokeScript("updateMarker", New Object() {getMarkerIndex(id), lat, lng}) End Sub Public Sub removeMarker(id As String) If markers.ContainsKey(id) Then markers.Remove(id) End Sub Public Function getMarkerIndex(id As String) As Integer Dim markerIndex As Integer = 0 For Each marker As KeyValuePair(Of String, MarkerData) In markers If marker.Key = id Then Return markerIndex markerIndex += 1 Next Return Nothing End Function Public Sub load() Dim contents As String = File.ReadAllText(".\map.html") Dim markerData As String = "" Dim markerId As Integer = 0 For Each marker As KeyValuePair(Of String, MarkerData) In markers Dim id As String = marker.Key Dim data As MarkerData = marker.Value Dim icon As String = data.getIcon() markerData &= "[" & data.getLat() & "," & data.getLng() & ",'" & id & If(icon = "", "", "','" & Path.GetFullPath(".\markers\").Replace("\", "/") & icon) & "']" & If(markerId = markers.Count - 1, "", ",") markerId += 1 Next Dim html As String = Path.GetFullPath(".\map_temp.html") File.WriteAllText(html, contents.Replace("[[MARKER_DATA]]", "[" & markerData & "]")) window.Navigate(html) End Sub End Class Public Class MarkerData Private lat As Double Private lng As Double Private icon As String Public Sub New(lat As Double, lng As Double, icon As String) Me.lat = lat Me.lng = lng Me.icon = icon End Sub Public Sub New(icon As String) Me.icon = icon End Sub Public Function getLat() As Double Return lat End Function Public Function getLng() As Double Return lng End Function Public Function getIcon() As String Return icon End Function End Class
Private Sub Me_Load() Handles MyBase.Load mapWindow.ObjectForScripting = Me map = New GoogleMap(mapWindow) map.registerMarker("Hi", "warning_yellow.png") map.load() tracker.Start() End Sub #Region "GeoLocation" Private WithEvents tracker As New GeoCoordinateWatcher() Private map As GoogleMap Private Sub tracker_PositionChanged(sender As Object, e As GeoPositionChangedEventArgs(Of GeoCoordinate)) Handles tracker.PositionChanged map.updateMarker("Hi", tracker.Position.Location.Latitude, tracker.Position.Location.Longitude) End Sub 'a forced update for testing Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click map.updateMarker("Hi", 20, 20) End Sub #End Region
формат HTML:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <link rel="stylesheet" type="text/css" href="map.css"> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> google.maps.event.addDomListener(window, 'load', function() { window.markers = new Array(); window.marker_data = [[MARKER_DATA]]; window.gmap = new google.maps.Map(document.getElementById('gmap'), { zoom: 12, center: new google.maps.LatLng(marker_data[0][0], marker_data[0][1]), mapTypeId: google.maps.MapTypeId.ROADMAP, ... some styling } var infowindow = new google.maps.InfoWindow(); var newMarker; for(var i = 0; i < marker_data.length; i++) { if(marker_data[0].length == 2) { newMarker = new google.maps.Marker( { position: new google.maps.LatLng(marker_data[i][0], marker_data[i][1]), map: gmap }); } else if(marker_data[0].length == 3) { newMarker = new google.maps.Marker( { position: new google.maps.LatLng(marker_data[i][0], marker_data[i][1]), map: gmap, title: (marker_data[i][2]) }); } else { newMarker = new google.maps.Marker( { position: new google.maps.LatLng(marker_data[i][0], marker_data[i][1]), map: gmap, title: (marker_data[i][2]), icon: (marker_data[i][3]) }); } google.maps.event.addListener(newMarker, 'click', function(newMarker, i) { return function() { if(newMarker.title) { infowindow.setContent(newMarker.title); infowindow.open(gmap, newMarker); } gmap.setCenter(newMarker.getPosition()); } }(newMarker, i)); markers[i] = newMarker; } }); function focusMarkerFromIdx(id) { google.maps.event.trigger(markers[id], 'click'); } function updateMarker(index, lat, lng) { var pos = new google.maps.LatLng(lat, lng); window.markers[i].setPosition(pos); window.gmap.panTo(pos); } </script> </head> <body> <div id="gmap"></div> </body> </html>
Что я уже пробовал:
Отладка в течение нескольких часов, ручная проверка кода