android map overlay draw
I have made a map overlay class and have overridden the onTouchEvent method that looks like this :
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapV开发者_如何学编程iew)
{
//TO-DO here we will capture when the users
//has pointed a point to go..
if(event.getAction() != MotionEvent.ACTION_MOVE){
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Log.e("##################",
"screen touched: lat:"+(p.getLatitudeE6()/ 1E6)+"long:"+(p.getLongitudeE6()/ 1E6));
showMap((p.getLatitudeE6()/ 1E6),(p.getLongitudeE6()/ 1E6));
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}else
return false;
}
The problem is that the map is drawn every time I do a gesture on the map, I would like to do this only when I click on a spot on the map and not when I drag the map, is this approach correct? How to implement this?
Take a look at ACTION_DOWN and ACTION_UP here
They both tell you starting and stopping locations, so you could do a check like:
float x, y;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = event.getRawX();
y = event.getRawY();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (((x - event.getRawX()) < threshhold_for_x) &&
(y - event.getRawY()) < threshhold_for_y)) {
// touch was in same location
}
}
That's the general idea anyway, of course you need to reset x and y, and if they do a gesture and wind up in the same location it would trigger it...
You might be able to use getDownTime ()
to make it work better.
精彩评论