Get the center coordinates of map in Android Mapview
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;
}
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.
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();
API is slightly updated. Here's actual version:
val mapViewCenter = Point(
googleMapView.width / 2,
googleMapView.height / 2
)
val mapCenter: LatLng = googleMap.projection.fromScreenLocation(mapViewCenter)
精彩评论