Android Question Conversion of longitude and latitude into screen coordinates x, y

Lakhtin_V

Active Member
Licensed User
Longtime User
Hi all!
I used the example GPSExampleV2_4. My target is to make a click on the map and place there Icon and remember its coordinates. Coordinates of longitude and latitude I get without problems from MapView. But I can not put my icon correctly on the in the place where I clicked. I can not get the correct translation longitude and latitude into screen coordinates x, y.
B4X:
Sub CalcMapScales
   
'    MapScaleLat = 360 / Power(2, MapZoomLevel) / MapViewer.Height
'    MapScaleLng = MapScaleLat * CosD(MapCenter.Latitude)
    MapScaleLng =  Power(2, MapZoomLevel) * TileSize / 360
    MapScaleLat = MapScaleLng * CosD(MapCenter.Latitude)
    Log("Calculates the map scales MapZoomLevel " & MapZoomLevel & "   MapHeight " & MapViewer.Height )
    Log("Calculates MapScale  Lng " & MapScaleLng & "   Lat " & MapScaleLat )
    Log("Calculates MapCenter Lng " & MapCenter.Longitude & "   Lat " & MapCenter.Latitude)
End Sub

Sub MapViewer_Click(LatStr As String, LngStr As String)
    '    Log("MouseDown event of the MapViewer WebView defined in MapDisp")
    Private Lat As Double, Lng As Double, x As Float, y As Float, k As Float

    MapDispTimer.Enabled = True
    k=MapViewer.Width/256
    Lat = LatStr
    Lng = LngStr
    x = (Lng - MapCenter.Longitude)*MapScaleLng + MapViewer.Width / 2 - 2.5%y
    y = (MapCenter.Latitude - Lat)*MapScaleLat + MapViewer.Height / 2 - 2.5%y
'    x = (Lng - MapCenter.Longitude) / MapScaleLng + MapViewer.Width / 2
    '    y = (MapCenter.Latitude - Lat) / MapScaleLat + MapViewer.Height / 2
'    MapXCursor = (Lng - MapCenter.Longitude) / MapScaleLng + MapViewer.Width / 2
'    MapYCursor = (MapCenter.Latitude - Lat) / MapScaleLat + MapViewer.Height / 2
    If Img.IsInitialized=False Then
        Log("Curent MapScaleLng " & MapScaleLng & "   MapScaleLat " & MapScaleLat )
        Log("Curent MapCenter Lng " & MapCenter.Longitude & "   Lat " & MapCenter.Latitude)
        Log("Curent MapWidth " & MapViewer.Width & "   MapHeight " & MapViewer.Height)
        Log("Curent Lng x" & Lng & "   Lat y" & Lat & "   x " & x & "   y " & y)
        If File.Exists(File.DirAssets,"z_help.png")=True Then
            Img.Initialize(File.DirAssets,"z_help.png")
            lblImgTarget.Initialize("None")
            lblImgTarget.SetBackgroundImage(Img)
            Activity.AddView(lblImgTarget, x, y, 5%y, 5%y)
        End If
        Else
        Log("END MapScaleLng " & MapScaleLng & "   MapScaleLat " & MapScaleLat )
        Log("END MapCenter Lng " & MapCenter.Longitude & "   Lat " & MapCenter.Latitude)
        Log("END MapWidth " & MapViewer.Width & "   MapHeight " & MapViewer.Height)
        Log("END Lng x" & Lng & "   Lat y" & Lat & "   x " & x & "   y " & y)
        MapDispTimer.Enabled = True
        lblImgTarget.Top=y
        lblImgTarget.Left=x
        lblImgTarget.Invalidate
    End If
End Sub

x = (Lng - MapCenter.Longitude)*MapScaleLng + MapViewer.Width / 2 - 2.5%y
y = (MapCenter.Latitude - Lat)*MapScaleLat + MapViewer.Height / 2 - 2.5%y

this lines not work right
 

Lakhtin_V

Active Member
Licensed User
Longtime User
I try to work with maps and GPS first time. I found your example GPSExampleV2_4, I liked this example, I decided to use it. I have a Google map on my phone screen. Is your method GoogleMaps fundamentally different?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do?
Do you want the icon drawn on the screen or on the map?
If you draw it on the screen and you move the map the icon will not move.
The screen coordinates are calculated in the DrawCursor routine:
B4X:
MapXCursor = (Lng - MapCenter.Longitude) / MapScaleLng + MapViewer.Width / 2
MapYCursor = (MapCenter.Latitude - Lat) / MapScaleLat + MapViewer.Height / 2


The GPSExample program was written before the GoogleMaps library was added to B4A.
I haven't taken the time to update it with the GoogleMaps library.
 
Upvote 0
Top