Using OSM viewer as posted here:
I noticed problems (lines not being drawn as the clicks on the map) when drawing on the map when close to the 180 degrees meridian (date line) and especially when crossing that line, so for example when a line starts at Lng 178 and moves to Lng -178.
The problem seems to be with this Sub in the above mentioned project. I made an alteration and this has definitely helped, in that now I can draw a polygon across the date line and it shows on the map as intended. There are still problem though for example when moving up the zoom level or just moving the map.
There also was a problem with the below Sub (used in Sub LatLngToPoint) in that it produced wrong tile numbers if the Lng was again close to the 180 degrees meridian.
This was fixed with a simple function, CorrectTileNumber, posted below as well.
Altogether it seems dealing with map locations close to the 180 degrees meridian is quite tricky and I would be very interested if somebody
has dealt with this successfully or has some general idea how to deal with this.
Note that I am not using Google maps, but mentioned OSM viewer instead.
RBS
Open Street Map viewer - GPS
Hi, This b4Xlib contains a custom view (cvMap) which can display Open Street Map. The tiles are retrieved from the internet and cached in a database. You can add shapes and images on the map. UI : - Lat/lng Center of the map - Zoom Level - Compass Direction with rotation - Scale - Button...
www.b4x.com
I noticed problems (lines not being drawn as the clicks on the map) when drawing on the map when close to the 180 degrees meridian (date line) and especially when crossing that line, so for example when a line starts at Lng 178 and moves to Lng -178.
The problem seems to be with this Sub in the above mentioned project. I made an alteration and this has definitely helped, in that now I can draw a polygon across the date line and it shows on the map as intended. There are still problem though for example when moving up the zoom level or just moving the map.
B4X:
'return TMapPoint from TMapLatLng
Public Sub LatLngToPoint(aLatLng As TMapLatLng, strCallingSub As String) As TMapPoint
Dim d As Double
Dim dTileSize As Double = MapUtilities.cTileSize
Dim bLog As Boolean = (strCallingSub = "clsMapShapePolygon, Draw")
'https://www.netzwolf.info/osm/tilebrowser.html?marker.x=12&marker.y=189&tx=1&ty=0&tz=1&ts=256#tile
Dim v As B4XView = PointToTile(0, 0)
Dim txy As TMapTileXY = v.tag
Dim gpx As Double = (txy.fX + (-v.Left / dTileSize)) / fTilesCount
Dim gpy As Double = (txy.fy + (-v.top / dTileSize)) / fTilesCount
Dim tx As TMapTileNumberOffset = LngToTileXPlusOffset(aLatLng.fLng, fMap.fZoomLevel)
Dim ty As TMapTileNumberOffset = LatToTileYPlusOffset(aLatLng.fLat, fMap.fZoomLevel)
Dim ppx As Double = (tx.fTile + (tx.fOffset / dTileSize)) / fTilesCount
Dim ppy As Double = (ty.fTile + (ty.fOffset / dTileSize)) / fTilesCount
d = ppx - gpx
'------------------------------------------------------------------------------------------------------------------
'this has been added/altered to avoid wrong values (much too high) for p.fX when we have a large longitude eg > 175
'note that there still can be problems eg when moving up zoom levels or when moving the map
'------------------------------------------------------------------------------------------------------------------
If d > 1 Then
d = d - 1
End If
'this was the old code
'------------------------------
' Dim p As TMapPoint = MapUtilities.initPoint(fTilesCount * MapUtilities.cTileSize * (ppx - gpx), _
' fTilesCount * MapUtilities.cTileSize * (ppy - gpy))
Dim p As TMapPoint = MapUtilities.initPoint(fTilesCount * dTileSize * d, _
fTilesCount * dTileSize * (ppy - gpy))
If bLog Then
Log("==== LatLngToPoint, Calling sub: " & strCallingSub)
Log("==== aLatLng.fLng: " & aLatLng.fLng & ", aLatLng.fLat: " & aLatLng.fLat)
Log("==== fMap.fZoomLevel: " & fMap.fZoomLevel)
Log("==== v.Left: " & v.Left)
Log("==== v.Width: " & v.Width)
Log("==== dTileSize: " & dTileSize)
Log("==== txy.fX: " & txy.fX & ", txy.fY: " & txy.fY)
Log("==== gpx: " & gpx)
Log("==== gpy: " & gpy)
Log("==== tx.fTile : " & tx.fTile & ", tx.fOffset : " & tx.fOffset)
Log("==== ty.fTile : " & ty.fTile & ", ty.fOffset : " & ty.fOffset)
Log("==== ppx: " & ppx)
Log("==== ppy: " & ppy)
Log("==== ppx - gpx: " & (ppx - gpx))
Log("==== ppy - gpy: " & (ppy - gpy))
Log("==== d: " & d)
Log("==== fTilesCount: " & fTilesCount)
Log("==== p.fX: " & p.fX & ", p.fY: " & p.fY)
Log("=====================================================")
End If
Return p
End Sub
There also was a problem with the below Sub (used in Sub LatLngToPoint) in that it produced wrong tile numbers if the Lng was again close to the 180 degrees meridian.
This was fixed with a simple function, CorrectTileNumber, posted below as well.
B4X:
Public Sub LngToTileXPlusOffset(aLng As Double, aZoom As Int) As TMapTileNumberOffset
Dim v As Double = (aLng + 180) / 360 * Power(2, aZoom)
'Log("cvMap, LngToTileX, v: " & v)
Dim t As TMapTileNumberOffset
t.Initialize
Log("LngToTileXPlusOffset, aLng: " & aLng & ", Floor(" & v & "): " & Floor(v))
t.fTile = MapUtilities.CorrectTileNumber(Floor(v), aZoom)
Log("LngToTileXPlusOffset (after correcting), t.fTile: " & t.fTile)
t.fOffset = (v - Floor(v)) * MapUtilities.cTileSize
Return t
End Sub
Sub CorrectTileNumber(iX As Int, iZoom As Int) As Int
Dim iLimit As Int = Power(2, iZoom)
If iX > iLimit - 1 Then
Return iLimit - iX
End If
If iX < 0 Then
Return iLimit + iX
End If
Return iX
End Sub
Altogether it seems dealing with map locations close to the 180 degrees meridian is quite tricky and I would be very interested if somebody
has dealt with this successfully or has some general idea how to deal with this.
Note that I am not using Google maps, but mentioned OSM viewer instead.
RBS