Android Question [SOLVED] Get GPS coordinate of a point shown on an OpenStreetMap panel

adrianstanescu85

Active Member
Licensed User
Hello

I added an OpenStreetMap map inside a panel, which is working fine, and what I would need is to know the GPS coordinates (lat, long) of a certain point I'm specifying inside that panel.

Let's say the panel is 400 x 200 px, and I'd need to find out the GPS coordinates of the point shown inside that panel at 100 x 40 px at the current zoom level.

Does anyone know how to do that?

Thank you!
Adrian
 

adrianstanescu85

Active Member
Licensed User
Yes, I'm using OSMDroid 3.0.8. Can you point a short example please? So far I wasn't able to find one, maybe I looked in the wrong places so far.
Thank you!
 

adrianstanescu85

Active Member
Licensed User
Inline java would do well too... for now converting to 4.1 might be pretty troublesome in the time left on the telemedicine project I'm working on. If you could pass a few inline java lines just for this map projection it would help a lot! Thank you!
 

warwound

Expert
Licensed User
Ok it actually looks quite simple...

This is the inline java you need to add to any Activity that you need to use it in:

B4X:
#If JAVA
import org.osmdroid.util.GeoPoint;
import uk.co.martinpearman.b4a.osmdroid.util.wrappers.GeoPointWrapper;

public GeoPointWrapper FromPixels(org.osmdroid.views.MapView mapView, int x, int y){
	GeoPointWrapper wrapper=new GeoPointWrapper();
	wrapper.setObject((GeoPoint) mapView.getProjection().fromPixels(x, y));
	return wrapper;
}
#End If
And your b4a calling code should look something like:

B4X:
Dim NativeMe As JavaObject
NativeMe.InitializeContext

Dim GeoPoint1 As GeoPoint=NativeMe.RunMethod("FromPixels", Array As Object(MyMapView, XCoord, YCoord))

' now test if GeoPoint1 is correct
Martin.
 

adrianstanescu85

Active Member
Licensed User
I put the inline java at the end of the activity
Also put the NativeMe declaration in Process_Globals.
Set the InitializeContext in Activity_Create when ran the first time.
In the mean time the map was well working as already set.

I set a label with a LongClick event get the geopoint and then list the coordinates.

This is the error I'm receiving: "An error has occured in sub:amedicsef_labelpressscena_longclick (java line: 946) java.lang.ClassCastException: uk.co.martinpearman.b4a.osmdroid.util.weappers.GeoPointWrapper cannot be cast to org.osmdroid.util.GeoPoint Continue?"

Why could this happen?
 

adrianstanescu85

Active Member
Licensed User
The exact code is:
B4X:
Sub LabelPressScena_LongClick
    Dim GeoPoint1 As GeoPoint  =NativeMe.RunMethod("FromPixels", Array As Object(MV1, 50, 50))
   
    ToastMessageShow("Lat: " & GeoPoint1.Latitude & " ,Long: " & GeoPoint1.Longitude, False)
End Sub
where MV1 is a MapView object. Thought this might help.
 

warwound

Expert
Licensed User
Try this:

B4X:
#If JAVA
import org.osmdroid.util.GeoPoint;

public GeoPoint FromPixels(org.osmdroid.views.MapView mapView, int x, int y){
	return (GeoPoint) mapView.getProjection().fromPixels(x, y);
}
#End If
 
Top