How to Convert GeoPoint to Equipment Screen Point

I would like to convert GeoPoint to a screen point to identify all objects above the touch event. So, I tried this:

Projection projection = this.mapView.getProjection(); GeoPoint gie = new GeoPoint(lat, lon); Point po = new Point(); projection.toPixels(gie, po); 

But po.x and po.y are not screen coordinates, but the display coordinates in pixels instead of lat, lon.

On the Android developer website:

toPixels (GeoPoint in, android.graphics.Point out) Converts GeoPoint data to the coordinates of the screen pixel relative to the upper left corner of the MapView that provided this projection.

So, is it possible to convert it to the correct screen coordinates?

I want to know all x GeoPoint that are next to the touch + event, as in the above example:

 ---------------------------------------------------------------- (0,0) -----------> x | | | | | | | <-- My screen | + (touch event) | \/ x (my GeoPoint) | y | | ---------------------------------------------------------------- 

I get a touch event as follows:

 @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); 

Here, in this code, x and y are the real screen coordinates (hardware, not cards)

I know that I can also convert the x, y coordinates to GeoPoint to compare them with my GeoPoint, but due to the zoom level I cannot get what I want.

+6
source share
2 answers

I found a solution, I do not know the best way to get it, but it works!

I found thanks for your help @nickt, the getScreenRect () method, which returns the current screen borders in screen coordinates.

Here is the code:

 float pisteX; float pisteY; Projection projection = this.mapView.getProjection(); Point pt = new Point(); GeoPoint gie = new GeoPoint(latitude,longitude); Rect rec = mapView.getScreenRect(new Rect()); projection.toPixels(gie, pt); pisteX = pt.x-rec.left; // car X screen coord pisteY = pt.y-rec.top; // car Y screen coord 
+1
source

I do not know why you are using onTouchEvent, which is for an event that was not handled by the view. If you use onTouch (View v0, MotionEvent e), this will give you information about the view, and you can calculate the screen coordinates using the getLeft and getBottom methods. If you create a project with a map view that occupies the lower right edge of the screen, for example:

main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="220dp" android:layout_height="220dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:apiKey="Your API key" android:clickable="true" /> </RelativeLayout> 

Then run this code

 public class GoogleMapsTouchActivity extends MapActivity implements OnTouchListener { private MapController mMapCtrlr; private MapView mMapVw; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onResume() { super.onResume(); mMapVw = (MapView) findViewById(R.id.map); mMapVw.setReticleDrawMode(MapView.ReticleDrawMode.DRAW_RETICLE_OVER); mMapCtrlr = mMapVw.getController(); mMapCtrlr.setZoom(15); mMapCtrlr.setCenter(new GeoPoint(53500000, -3000000)); mMapVw.setOnTouchListener(this); } @Override public boolean onTouch(View v0, MotionEvent e) { // Only fires if Mapview touched float x = e.getX(); // relative to VIEW float y = e.getY(); // relative to VIEW float x2; // relative to SCREEN float y2; // relative to SCREEN if (e.getAction() == MotionEvent.ACTION_DOWN) { Toast.makeText(this, "View x = " + x + " View y = " + y, Toast.LENGTH_SHORT).show(); x2 = mMapVw.getLeft() + x; y2 = mMapVw.getBottom() + y; Toast.makeText(this, "Screen x = " + x2 + " Screen y = " + y2, Toast.LENGTH_SHORT).show(); GeoPoint gPt = mMapVw.getProjection().fromPixels((int) x, (int)y); Toast.makeText(this, "Lat = " + gPt.getLatitudeE6() + " Lon = " + gPt.getLongitudeE6(), Toast.LENGTH_SHORT).show(); } return false; } @Override public boolean onTouchEvent(MotionEvent e) { // Only fires if screen touched outside a view float x = e.getX(); float y = e.getY(); if (e.getAction() == MotionEvent.ACTION_DOWN) { Toast.makeText(this, "Got touch EVENT x = " + x + " y = " + y, Toast.LENGTH_SHORT).show(); } return super.onTouchEvent(e); } @Override protected boolean isRouteDisplayed() {return false;} } 

If you click on a view and an area outside of mapview, Toast posts should make everything clear to you.

+2
source

Source: https://habr.com/ru/post/916251/


All Articles