3

In my application I'm trying to get the center coordinates of map when user scroll around the map.

I want to get the co-ordinates and set it on the text view.

Here is my code for that:

public boolean onTouchEvent(MotionEvent event) {
        int action=event.getAction();
        projection=mapView.getProjection();
        int X = (int)event.getX();          
        int Y = (int)event.getY();
        if(action==MotionEvent.ACTION_MOVE)
        {


            metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            GeoPoint G = projection.fromPixels(metrics.heightPixels/2, metrics.widthPixels/2);
        //GeoPoint p=   mapView.getMapCenter();
            int lati=p.getLatitudeE6();
            Log.i("Lat : ",""+lati);
            Toast.makeText(this,""+lati,Toast.LENGTH_LONG);

            int longi=p.getLongitudeE6();
            Log.i("Lon : ",""+longi);
            Toast.makeText(this,""+longi,Toast.LENGTH_LONG);
            lat.setText(""+lati);
            lon.setText(""+longi);
        }
        return true;
    }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
android
  • 386
  • 1
  • 8
  • 21

3 Answers3

4

Use this article:

http://mobiforge.com/developing/story/using-google-maps-android

It will answer all of your questions about using mapview.(mostly all). Plus for your particular need:

search for "Getting the Location that was touched" on the page.

Jakob Harteg
  • 9,587
  • 15
  • 56
  • 78
mudit
  • 25,306
  • 32
  • 90
  • 132
  • 1
    did you find out how to do it? I am trying to achieve the same thing. Thanks – Thiago Aug 25 '11 at 18:42
  • 1
    @Thiago The question has already been updated with the answer. Similar to what I'm using: projection=mapView.getProjection(); GeoPoint coordinatesOfMapCenter = projection.fromPixels(mapView.getWidth()/2, mapView.getHeight()/2); – Stan Kurdziel May 11 '12 at 21:39
2

The OP included the correct answer in his question. This is how to do it:

GeoPoint mapCenter = mapView.getProjection().fromPixels(
        mapView.getWidth()/2,
        mapView.getHeight()/2);

int lat = mapCenter.getLatitudeE6();
int lon = mapCenter.getLongitudeE6();
sulai
  • 5,204
  • 2
  • 29
  • 44
0

API is slightly updated. Here's actual version:

val mapViewCenter = Point(
    googleMapView.width / 2,
    googleMapView.height / 2
)
val mapCenter: LatLng = googleMap.projection.fromScreenLocation(mapViewCenter)
Taras Lozovyi
  • 876
  • 9
  • 15