Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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


VB
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


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
Posted
Updated 2-Jul-17 6:26am

1 solution

Solved: silly mistake;
window.markers[i].setPosition(pos);

should be
window.markers[index].setPosition(pos);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900