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

Discussion in 'Android Questions' started by adrianstanescu85, May 24, 2015.

  1. adrianstanescu85

    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
     
  2. warwound

    warwound Expert Licensed User

    Are you using OSMDroid?
    If so look at the OSMDroid_Projection object.
     
  3. adrianstanescu85

    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!
     
  4. warwound

    warwound Expert Licensed User

    :( Version 3.0.8 never had the map projection wrapped/exposed to b4a.
    Only version 4.1 wraps the map projection into a b4a object:
    http://b4a.martinpearman.co.uk/osmdroid/

    Maybe you could use some inline java to access the map projection using version 3.0.8.
    I'll have a look at the documentation for you 3.0.8 tomorrrow.
     
    MarcoRome and thedesolatesoul like this.
  5. adrianstanescu85

    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!
     
    thedesolatesoul likes this.
  6. warwound

    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:

    Code:
    #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:

    Code:
    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.
     
    thedesolatesoul likes this.
  7. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    I'll test it right now and let you know how it works! Thank you!
     
  8. adrianstanescu85

    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?
     
    thedesolatesoul likes this.
  9. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    The exact code is:
    Code:
    Sub LabelPressScena_LongClick
        
    Dim GeoPoint1 As GeoPoint  =NativeMe.RunMethod("FromPixels"Array As Object(MV1, 5050))
       
        
    ToastMessageShow("Lat: " & GeoPoint1.Latitude & " ,Long: " & GeoPoint1.Longitude, False)
    End Sub
    where MV1 is a MapView object. Thought this might help.
     
    thedesolatesoul likes this.
  10. warwound

    warwound Expert Licensed User

    Try this:

    Code:
    #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
     
    thedesolatesoul likes this.
  11. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    Yes, this works!

    Wonderfull!

    Thank you so much!!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice