brajabasi / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

OpenStreetMapProjection.fromPixels(float, float) method doesn't work properly. #52

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
public GeoPoint fromPixels(float x, float y) {
                        return
bb.getGeoPointOfRelativePositionWithLinearInterpolation(x / viewWidth_2, y
                                        / viewHeight_2);
}

When I try to get coordinates of current map center -> I'll get coordinates
of bottom-right corner.

I fixed this by the following (we should divide by view width and view
height, but not by viewWidth/2, viewHeight/2 to get relative position on a
point):

bb.getGeoPointOfRelativePositionWithLinearInterpolation(x / viewWidth_2/2, y
                                        / viewHeight_2/2);

What version of the product are you using? On what operating system?
Trunk, rev. 162.

Original issue reported on code.google.com by vitaliy....@gmail.com on 10 May 2010 at 4:00

GoogleCodeExporter commented 8 years ago
That doesn't quite make sense to me. viewWidth_2 is getWidth() / 2, so now 
you're dividing by 2 again, 
or multiplying by 2. How are you using it so that it's not working?

I'm using fromPixels in my application and it works fine for me.

PS. The next method, fromMapPixels, contains this piece of code:
x - getWidth() / 2
which should probably be x - viewWidth_2

Original comment by neilboyd on 10 May 2010 at 7:58

GoogleCodeExporter commented 8 years ago
AFAIK, relative position of point is between [0..1] for X and Y coordinates.
E.g. 
1) top left corner of a screen has {x,y}={0,0} relative coordinates.
2) bottom right corner of a screen has {x,y}={1,1} relative coordinates.

if x1=MotionEvent.getX() and y1=MotionEvent.getY() (let it be center of a 
screen).
then if I pass x1, y1 to OpenstreetMapProjection.fromPixels(x1, y1) I'll get 
GeoPoint
with coordinates of bottom right corner of the screen.

This is because relative position is not calculated correctly here:

bb.getGeoPointOfRelativePositionWithLinearInterpolation(x / viewWidth_2, y
                                        / viewHeight_2);

x / viewWindth_2 is between [0..2], but should be between [0..1]
y / viewHeight_2 is between [0..2], but should be between [0..1]

Please correct me, if I'm wrong and show how to get LatLon coordinates of some
MotionEvent.

Original comment by vitaliy....@gmail.com on 10 May 2010 at 8:53

GoogleCodeExporter commented 8 years ago
I call it like this:

GeoPoint topLeft = projection.fromPixels(0, 0);
GeoPoint bottomRight = projection.fromPixels(mapView.getWidth(), 
mapView.getHeight());

I don't use it with a MotionEvent, but I guess it should work the same.

Original comment by neilboyd on 11 May 2010 at 7:14

GoogleCodeExporter commented 8 years ago
Hi! I'm trying to do something like:

animateTo(mapview.getProjection().fromPixels(event.getX(),event.getY()));

but it seems the method fromPixels() doesn't work like it should. The map will 
center to a very different point.

P.S: I'm using the jar 1.02.

Original comment by cat...@gmail.com on 6 Jul 2010 at 2:14

GoogleCodeExporter commented 8 years ago
I also discovered basicly the same problem in using Projection.fromPixels():

        OpenStreetMapViewProjection projection = openStreetMapView.getProjection();
        GeoPoint newTopLeft = projection.fromPixels(0, 0);
        GeoPoint newBottomRight = projection.fromPixels(openStreetMapView.getRight(), 
                openStreetMapView.getBottom());

I guess the gps area returned is 2-4 times the screen view.

I noted the Projection is only created in OpenStreetMapView.onDraw(). Is it 
possible that getProjection() doesn't get the most current settings the way we 
use it? (This is also a problem when getting the settings before the view has 
been drawn the first time.) I haven't looked into the code enough to say 
anything for sure but it seems better for onDraw() to be free of side-effects.

This is with trunk r269.

Original comment by andpet@gmail.com on 6 Jul 2010 at 5:11

GoogleCodeExporter commented 8 years ago
This issue was closed by revision r283.

Original comment by neilboyd on 7 Jul 2010 at 1:00

GoogleCodeExporter commented 8 years ago
I finally figured out the point vitaliy.grigoruk was making ;-)
Sorry it took so long, but the code in comment 4 made it obvious.

Original comment by neilboyd on 7 Jul 2010 at 1:03