'Activity module
Sub Process_Globals
Dim ScaleLat, ScaleLong, Scale_Distance As Double
Dim TileWidth, TileHeight As Int
Dim ZoomLevel As Int
Dim PosLat, PosLng As List
Type CoordLongLat(Lng As Double, Lat As Double)
Dim CenterLonLat As CoordLongLat
Dim DispMapTypeControl As Boolean : DispMapTypeControl = True
Dim DispMapScaleControl As Boolean : DispMapScaleControl = True
Dim DispMapZoomControl As Boolean : DispMapZoomControl = True
Dim DispMapCenterMarker As Boolean : DispMapCenterMarker = True
Dim DispMapMarkers As Boolean : DispMapMarkers = True
Dim DispMapPolyline As Boolean : DispMapPolyline = True
Dim Http As HttpClient
Dim GPS1 As GPS
End Sub
Sub Globals
Dim MapViewer As WebView
Dim MapViewerExtra As WebViewExtras
Dim lblLatitude, lblLongitude, lblZoom, lblCenterLat, lblCenterLng As Label
Dim lblLon As Label
Dim lblLat As Label
Dim lblSatellites As Label
Dim aufruf As Boolean
aufruf = False
Dim myLat As Double
Dim myLon As Double
' added a new variable (Martin)
Dim mapIsInitialized As Boolean : mapIsInitialized=False
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Http.Initialize("Http")
End If
If FirstTime Then
GPS1.Initialize("GPS")
End If
Activity.LoadLayout("map")
PosLat.Initialize
PosLng.Initialize
InitTestCoordinates
MapViewer.Initialize("")
Activity.AddView(MapViewer, 0, 0, 100%x, 100%y)
MapViewer.Tag = "MapViewer"
MapViewerExtra.addJavascriptInterface(MapViewer, "B4A")
' temporarily add a WebChromeClient to the WebView
' if any javascript errors occur they will be visible in the log
' remove the WebChromeClient when it's no longer required
MapViewerExtra.addWebChromeClient(MapViewer, "")
lblLatitude.Initialize("")
Activity.AddView(lblLatitude, 0, 100%y - 40dip, 35%x, 40dip)
lblLatitude.TextColor = Colors.Red
lblLongitude.Initialize("")
Activity.AddView(lblLongitude, 35%x, 100%y - 40dip, 35%x, 40dip)
lblLongitude.TextColor = Colors.Red
lblZoom.Initialize("")
Activity.AddView(lblZoom, 70%x, 100%y - 40dip, 30%x, 40dip)
lblZoom.TextColor = Colors.Red
lblCenterLat.Initialize("")
Activity.AddView(lblCenterLat, 0, 100%y - 70dip, 50%x, 30dip)
lblCenterLat.TextColor = Colors.Red
lblCenterLng.Initialize("")
Activity.AddView(lblCenterLng, 50%x, 100%y - 70dip, 50%x, 30dip)
lblCenterLng.TextColor = Colors.Red
End Sub
Sub Activity_Resume
If GPS1.GPSEnabled = False Then
ToastMessageShow("Bitte GPS einschalten.", True)
StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
GPS1.Start(0, 0) 'Listen to GPS with no filters.
End If
' ShowMap
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub GPS_LocationChanged (Location1 As Location)
myLat = Location1.Latitude
myLon = Location1.Longitude
Log(myLat)
Log(myLon)
lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
' lblSpeed.Text = "Speed = " & Location1.Speed
' Dim Location2 As Location
'Location2.Initialize2("51:37.296", "8:41.626")
' Location2.Initialize2("51:38.130", "8:40.004")
' lbldis.Text = "Distance = " & Location1.DistanceTo(Location2)
' lblgrad.Text = "Grad = " & Location1.BearingTo (Location2)
If mapIsInitialized Then
' change the Map and Marker positions (Martin) - only if they have been created
Dim Javascript As String
Javascript="locationChanged("&myLat&","&myLon&")"
MapViewerExtra.executeJavascript(MapViewer, Javascript)
End If
End Sub
Sub GPS_UserEnabled (Enabled As Boolean)
' ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub
Sub GPS_GpsStatus (Satellites As List)
lblSatellites.Text = "Satellites:" & CRLF
For i = 0 To Satellites.Size - 1
Dim Satellite As GPSSatellite
Satellite = Satellites.Get(i)
lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
" " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
& " " & Satellite.Elevation
Next
If aufruf = False Then
If lblLat.Text <> "Bitte warten" Then
If lblLon.Text <> "Bitte warten" Then
Msgbox("Aufruf","")
InitTestCoordinates
ShowMap
aufruf = True
End If
End If
Else
MapViewer_CenterChanged(NumberFormat(myLat, 1, 6), NumberFormat(myLon, 1, 6))
End If
End Sub
Sub ShowMap
DispMap(CenterLonLat.Lat, CenterLonLat.Lng, ZoomLevel, DispMapTypeControl, DispMapZoomControl, "LEFT_TOP", DispMapScaleControl, "TOP_LEFT", DispMapCenterMarker, PosLat, PosLng, DispMapMarkers, DispMapPolyline, "ff0000", 0.5, 3)
End Sub
Sub DispMap(CenterLat As Float, CenterLong As Float, Zoom As Int, MapTypeControl As Boolean, MapZoomControl As Boolean, ZoomControlPosition As String, MapScaleControl As Boolean, ScaleControlPosition As String, DispMarkerCenter As Boolean, MarkerLat As List, MarkerLong As List, DispMarkers As Boolean, DispPolyline As Boolean, PolyLineColor As String, PolyLineOpacity As Float, PolyLineWidth As Int)
' CenterLat = latitude of map center in degrees
' CenterLong = longitude of map center in degrees
' Zoom = zomm index 0 - 21
' MapTypeControl = true displays the map type control
' DispZoomControl = true displays the zoom control otherwise false
' ZoomControlPosition = position of the zoom control TOP_LEFT, TOP_CENTER, TOP_RIGHT, LEFT_CENTER, RIGHT_CENTER, BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT
' ScaleControl = true displays the zoom control otherwise false
' ScaleControlPosition = position of the scale control TOP_LEFT, TOP_CENTER, TOP_RIGHT, LEFT_CENTER, RIGHT_CENTER, BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT
' DispMarkerCenter = true sets a marker on the center of the map
' MarkerLat = List of lat positions of the markers
' MarkerLong = List of long positions of the markers
' DipsMarkers = true displays the markers
' DispPolyline = true displays a polyline with the markers as vertices
' PolyLineColor = polyline color hexadecimal #ff0000 = red #00ff00 = green #0000ff = blue
' PolyLineOpacity = polyline opacity 0.0 transparent 1.0 full opaque
' PolyLineWidth = polyline width in pixels
Dim HtmlCode As String
Dim i, j As Int
' added global variables and locationChanged function to this line (Martin)
HtmlCode = "<!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: 0px; padding: 0px }#map_canvas { height: 100% }</style><script type='text/javascript' src='http://maps.google.com/maps/api/js?sensor=true'></script><script type='text/javascript'>var markerc, map; function locationChanged(latitude, longitude){var latLng=new google.maps.LatLng(latitude, longitude);markerc.setPosition(latLng);map.panTo(latLng);}function initialize() {var centerlatlng = new google.maps.LatLng(" & CenterLat & "," & CenterLong & "); var myOptions = { zoom: "&Zoom&", center: centerlatlng, disableDefaultUI: true, zoomControl: "& MapZoomControl & ", zoomControlOptions: {position: google.maps.ControlPosition." & ZoomControlPosition & "}, scaleControl: "& MapScaleControl & ", scaleControlOptions: {position: google.maps.ControlPosition." & ScaleControlPosition & "}, mapTypeControl: "& MapTypeControl& ", mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map_canvas'), myOptions)"
' add click event
HtmlCode = HtmlCode & ";google.maps.event.addListener(map, 'click', function(mouseEvent){"
HtmlCode = HtmlCode & "var lat = mouseEvent.latLng.lat();"
HtmlCode = HtmlCode & "var lng = mouseEvent.latLng.lng();"
HtmlCode = HtmlCode & "B4A.CallSub('MapViewer_Click', true, lat, lng); })"
' add zoom_changed event
HtmlCode = HtmlCode & ";google.maps.event.addListener(map, 'zoom_changed', function() {"
HtmlCode = HtmlCode & "var zoomLevel = map.getZoom();"
HtmlCode = HtmlCode & "B4A.CallSub('MapViewer_ZoomChanged', true, zoomLevel);"
HtmlCode = HtmlCode & " })"
' add center_changed event
HtmlCode = HtmlCode & ";google.maps.event.addListener(map, 'center_changed', function() {"
HtmlCode = HtmlCode & "var centerlatlng = map.getCenter();"
' HtmlCode = HtmlCode & "B4A.CallSub('MapViewer_CenterChanged', true, centerlatlng.lat(), centerlatlng.lng() );"
HtmlCode = HtmlCode & "B4A.CallSub('MapViewer_CenterChanged', true, centerlatlng.lat(), centerlatlng.lng() );"
HtmlCode = HtmlCode & " })"
' displays a marker on the map center
If DispMarkerCenter = True Then
' removed var from this line (Martin)
HtmlCode = HtmlCode & "; markerc = new google.maps.Marker({ position: new google.maps.LatLng(" & CenterLat & "," & CenterLong & "),map: map, title: '',clickable: false,icon: 'http://www.google.com/mapfiles/arrow.png' })"
End If
' displays markers on the map
If MarkerLat.Size > 0 Then
j = MarkerLat.Size - 1
If DispMarkers = True Then
If j = 0 Then
HtmlCode = HtmlCode & "; var marker0 = new google.maps.Marker({ position: new google.maps.LatLng(" & MarkerLat.Get(j) & "," & MarkerLong.Get(j) & "),map: map, title: 'Test0',clickable: true, draggable: true, icon: 'http://www.google.com/mapfiles/marker_red.png' })"
' HtmlCode = HtmlCode & "; google.maps.event.addListener(marker0, 'click', function() {alert('Marker1')} )"
Else If j = 1 Then
HtmlCode = HtmlCode & "; var marker0 = new google.maps.Marker({ position: new google.maps.LatLng(" & MarkerLat.Get(0) & "," & MarkerLong.Get(0) & "),map: map, title: 'Test0',clickable: true, draggable: true, icon: 'http://www.google.com/mapfiles/marker_green.png' })"
' HtmlCode = HtmlCode & "; google.maps.event.addListener(marker0, 'click', function() {map.set_center(marker" & i & ")} )"
HtmlCode = HtmlCode & "; var marker1 = new google.maps.Marker({ position: new google.maps.LatLng(" & MarkerLat.Get(1) & "," & MarkerLong.Get(1) & "),map: map, title: 'Test1',clickable: true, draggable: true, icon: 'http://www.google.com/mapfiles/marker.png' })"
' HtmlCode = HtmlCode & "; google.maps.event.addListener(marker1, 'click', function() {map.set_center(marker" & i & ")} )"
Else
HtmlCode = HtmlCode & "; var marker0 = new google.maps.Marker({ position: new google.maps.LatLng(" & MarkerLat.Get(0) & "," & MarkerLong.Get(0) & "),map: map, title: 'Test0',clickable: true, draggable: true, icon: 'http://www.google.com/mapfiles/marker_greenA.png' })"
' HtmlCode = HtmlCode & "; google.maps.event.addListener(marker0, 'click', function() {alert('Marker0')} )"
For i = 1 To j - 1 ' diplays the markers
HtmlCode = HtmlCode & "; var marker" & i & " = new google.maps.Marker({ position: new google.maps.LatLng(" & MarkerLat.Get(i) & "," & MarkerLong.Get(i) & "),map: map, title: 'Test" & i & "',clickable: true, draggable: true, icon: 'http://www.google.com/mapfiles/marker_orange" & Chr(i + 65) & ".png' })"
' HtmlCode = HtmlCode & "; google.maps.event.addListener(marker" & i & ", 'click', function() {map.set_center(marker" & i & ")} )"
Next
HtmlCode = HtmlCode & "; var marker" & (j) & " = new google.maps.Marker({ position: new google.maps.LatLng(" & MarkerLat.Get(j) & "," & MarkerLong.Get(j) & "),map: map, title: 'Test" & j & "',clickable: true, draggable: true, icon: 'http://www.google.com/mapfiles/marker" & Chr(j + 65) & ".png' })"
' HtmlCode = HtmlCode & "; google.maps.event.addListener(marker" & j & ", 'click', function() {map.set_center(marker" & j & ")} )"
End If
End If
' displays a polyline between the markers
If DispPolyline = True AND j > 0 Then
HtmlCode = HtmlCode & "; var points = ["
HtmlCode = HtmlCode & " new google.maps.LatLng(" & MarkerLat.Get(0) & "," & MarkerLong.Get(0) & ")"
For i = 1 To j
HtmlCode = HtmlCode & ", new google.maps.LatLng(" & MarkerLat.Get(i) & "," & MarkerLong.Get(i) & ")"
Next
HtmlCode = HtmlCode & "] "
HtmlCode = HtmlCode & "; var polyline = new google.maps.Polyline({path: points, strokeColor: '" & PolyLineColor & "', strokeOpacity: " & PolyLineOpacity & ", strokeWeight: " & PolyLineWidth & "})"
HtmlCode = HtmlCode & "; polyline.setMap(map)"
End If
End If
' the javascript will now notify the B4A code once the Map and Marker are created
HtmlCode=HtmlCode & "; B4A.CallSub('MapViewer_SetInitialized', true)"
HtmlCode = HtmlCode & "; }</script></head><body onload='initialize()'> <div id='map_canvas' style='width:100%; height:100%'></div></body></html>"
MapViewer.LoadHtml(HtmlCode)
File.WriteString(File.DirRootExternal, "tudorf_map.htm", HtmlCode)
End Sub
Sub MapViewer_Click(LatStr As String, LngStr As String)
Dim Lat As Double
Dim Lng As Double
Lat=LatStr
Lng=LngStr
lblLatitude.Text = " Lat = " & NumberFormat(myLat, 1, 6)
lblLongitude.Text = " Lng = " & NumberFormat(myLon, 1, 6)
End Sub
Sub MapViewer_ZoomChanged(ZoomL As String)
ZoomLevel = ZoomL
lblZoom.Text = "Zoom = " & ZoomLevel
End Sub
Sub MapViewer_CenterChanged(CenterLat As String, CenterLng As String)
CenterLonLat.Lat = CenterLat
CenterLonLat.Lng = CenterLng
lblCenterLat.Text = " CenterLat = " & NumberFormat(CenterLat, 1, 6)
lblCenterLng.Text = " CenterLng = " & NumberFormat(CenterLng, 1, 6)
End Sub
Sub InitTestCoordinates
' PosLat.Add(46.1348)
' PosLng.Add(7.113)
' PosLat.Add(46.1363)
' PosLng.Add(7.1122)
' PosLat.Add(46.1379)
' PosLng.Add(7.1152)
' PosLat.Add(46.1386)
' PosLng.Add(7.1148)
' PosLat.Add(46.1395)
' PosLng.Add(7.1171)
' PosLat.Add(46.1409)
' PosLng.Add(7.1188)
' PosLat.Add(46.1400)
' PosLng.Add(7.1201)
' CenterLonLat.Lat = 46.1379
' CenterLonLat.Lng = 7.1163
CenterLonLat.Lat = NumberFormat(myLat, 1, 6)
CenterLonLat.Lng = NumberFormat(myLon, 1, 6)
ZoomLevel = 15
End Sub
' a new Sub which will be called by the javascript once the Map and Marker have been created
Sub MapViewer_SetInitialized
mapIsInitialized=True
End Sub