4

I am trying to convert screen coordinates to a Geopoint in android mapview. I need to show a marker at the center of screen(mapview is filled in the window). So I tried to get the center coordinates using display metrics getWidth() and getHeight() methods and converted to geoppoint using mapView.getProjection().fromPixels(height/2, width/2). But it seems to be giving wrong results. What shall I do?

althaf_tvm
  • 773
  • 3
  • 15
  • 28

2 Answers2

5

You have passing the height and width argument as wrong change

mapView.getProjection().fromPixels(width/2,height/2);

try this code

Projection proj = mapview.getProjection();
GeoPoint proj.fromPixels(int x, int y) ;

here you have to pass the x,y coordinate and it will convert to the GeoPoint you can also reserve this from GeoPoint to pixel also from this object

Point p = new Point();
proj.toPixels(GeoPoint in, android.graphics.Point out) 

check this link for more info

https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/Projection

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

Are you taking into account the notificationBar size? maybe also the appBar?

What you want to know is the width and height of the MapView, and then get the projection :)

halfer
  • 19,824
  • 17
  • 99
  • 186
Goofyahead
  • 5,874
  • 6
  • 29
  • 33