iOS Question GoogleMap CoordinateFromPoint

marcick

Well-Known Member
Licensed User
Longtime User
Hi all,
How can I obtain the coordinates of a point that is, for example, at 50% horizontally and 25% vertically on the map view, regardless of the current bearing?
 

emexes

Expert
Licensed User
Longtime User
Do you know the coordinates of the corners of the map image? Linear interpolation might be close enough, if you're not at the poles.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
how can I get to corners coordinates (remember, the map is rotated) ?

Lol I don't know, I was hoping you knew.

What do you know about the map view? Like, how do you specify what part of the world is to be shown in the (presumably) rectangular viewport, presumably defined as x1..x2 and y1..x2 on the screen? I imagined that you'd need to specify (for example) the longitude and latitude of the centre of the viewport, the scale of the map in say metres-per-pixel, and the rotation of the view, with angle 0 being north-at-top.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I can tell you I went crazy for days, even with ChatGPT’s help, trying to get the coordinates of the visible map area at a given zoom level and bearing, but without success.
The goal I need to achieve is: once I have a marker centered on the map with a certain zoom and bearing, I want to move it 25% downward on the screen.
I managed to do it in B4A, but not in B4i.
The main obstacle is right at the beginning: retrieving the coordinates of the corners of the visible map…

This is the B4A code, that I'm not able to translate for B4i (the MapCalc is a hidden map used to move the camera to te final destination without a double animation, first at cneter, then 25% down)

B4X:
Sub GetLatLngAt25PercentFromBottom(markerPos As LatLng, zoom As Float, bearing As Float) As ResumableSub
    ' Step 1: center the map on the marker with the given bearing and zoom
    Dim cp As CameraPosition
    cp.Initialize2(markerPos.Latitude, markerPos.Longitude, zoom, bearing, 0)
    gmapcalc.MoveCamera(cp)
    ' Step 2: wait until the invisible map has finished moving
    Wait For (WaitMapIdle) Complete (ok As Boolean)
    ' Step 3: calculate the screen coordinates of the point at 25% from the bottom
    Dim mapWidth As Int = PnlMapCalc.Width ' panels containing the invisible map
    Dim mapHeight As Int = PnlMapCalc.Height
    Dim screenX As Int = mapWidth / 2
    Dim screenY As Int = mapHeight * 0.25
    Dim joMap As JavaObject = gmapcalc
    Dim projection As JavaObject = joMap.RunMethod("getProjection", Null)
    Dim pointClass As JavaObject
    pointClass.InitializeNewInstance("android.graphics.Point", Array(screenX, screenY))
    Dim targetLatLng As JavaObject = projection.RunMethod("fromScreenLocation", Array(pointClass))
    Dim result As LatLng
    result.Initialize(targetLatLng.GetField("latitude"), targetLatLng.GetField("longitude"))
    Return result
End Sub
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Longtime User
retrieving the coordinates of the corners of the visible map…

What's the name of the map view class that you're using? What properties are listed when you're in the IDE and type the name of the map view followed by a full stop (decimal point, period) ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Actual googlemap has no property useful for my needings, I think something has to be written with NativeObject like methods in GoogleMapExtra (but ChatGpt suggested me maybe 50 solutions and no one working ....)
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Actual googlemap has no property useful for my need

After looking through:


I am inclined to agree with you. Seems odd, though: surely it must be a common need.

I was thinking maybe at least markers would reveal their screen coordinates, but... no.

There was a new map library by @Star-Dust, I think it uses OpenStreetMap instead of Google Map, to get around some new limit or charge of using the Google Map API, so have a look at that, see if it's got any link to screen coordinates.

(I'm about to sign off for the night here)
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thanks anyway for the support. No, I don't want to change GoogleMaps with other. And there is no limit of usage, no charge usino googlemaps insider the app.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Righto, don't laugh or call me an idiot, but here's a possible solution of last resort:

Grab a screenshot bitmap of the current map view
Add a marker at the central lat-lon that the camera is looking at
Grab another screenshot bitmap of the map view
Do a pixel-by-pixel comparison of the two screenshots to find the screen coordinates that match that lat-lon
Move the marker ~1 metre north, see if it moves on-screen, if not, keep doubling the move distance until the marker has moved say 20 pixels
Note that new lat-lon and the corresponding screen coordinates
Do the same for south, east and west (or for NE, NW, SE and SW, to get corners of a rectangle)
Given those four matching points, should be able to convert (nearby) lat-lon to screen coordinates and vice-versa

Seems doable, as long as it's possible to grab a screenshot bitmap or similar of the map view.

What could possibly go rwong? 🍻
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
There was a new map library by @Star-Dust, I think it uses OpenStreetMap instead of Google Maps

jSD_OpenMaps
  • OpenMaps, OpenMapsExt
    • Functions:
      • LatLonToXY (ll As LatLng) As Double()
        Returns the screen coordiantes for the given LatLng coordinates
      • XYToLatLng (x As Double, y As Double) As LatLng
        Returns the Lat/Lng coordinates for the given screen coordinates 'doesn't work!!!

Part of the reason for pointing you to the @Star-Dust library for OpenStreetMap was that (I think) it started as a drop-in replacement for the Google Maps library, and so if we could find the functions you need in that, then we'd have an idea of where to go looking in the Google Maps library.

So far, 50% success. Found the function names in the OpenStreetMap library, but can't find those same names or similar in the Google Maps library.

Close, but no cigar.

Also: the 'doesn't work!!! comment doesn't bode well.[/b]
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thankg again. After a deep search on the forum I've found the method that drive me to the solution:

B4X:
    Dim bounds As Object = gextra.GetVisibleBounds
    Dim ne_sw() As LatLng = gextra.GetBoundsNE_SW(bounds)

I suppose the bounds are valid when the map bearing is 0, but But now it's just a matter of some trigonometric calculations.
 
Upvote 0

Similar Threads

Top