本文介绍了更新Google地图标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好b $ b
我正试图通过VB.NET移动现有标记的位置。编译器或html中没有错误,没有任何反应。现有的标记保持不变。
我将标记数据存储在Dictionary(Of String,MarkerData)中,其中String是Id(Hi 在此示例中)和MarkerData包含标记的纬度,经度和图标路径。
Hi
I'm trying to move an existing marker's position through VB.NET. There are no errors in the compiler or the html, and nothing happens whatsoever. The existing marker stays where it is.
I'm storing the marker data in a Dictionary(Of String, MarkerData), where the String is the Id ("Hi" in this example) and MarkerData contains the latitude, longitude and icon path for the marker.
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:
The 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>
我尝试过:
调试几个小时,手动检查代码
What I have tried:
Debugging for a few hours, manually inspecting code
推荐答案
window.markers[i].setPosition(pos);
应为
should be
window.markers[index].setPosition(pos);
这篇关于更新Google地图标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!