Here in this blog i implement Itemized-overlay with handling onTap and onTouchevent.
As shown below onTap method is used for pop-up and onTouch method is used for drag and drop pin at particular location.
public class MapDemoActivity extends MapActivity {
private MapView map = null;
private MyLocationOverlay me = null;
List<Overlay> mapOverlays;
boolean flag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_demo);
map = (MapView) findViewById(R.id.map);
map.getController().setCenter(
getPoint(40.76793169992044, -73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
mapOverlays = map.getOverlays();
Drawable marker = getResources().getDrawable(R.drawable.mekr);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker, map));
me = new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
public MapView getMapView() {
return this.map;
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
@Override
protected boolean isRouteDisplayed() {
return (false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return (true);
} else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return (true);
}
return (super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();
private Drawable marker = null;
private OverlayItem inDrag = null;
private ImageView dragImage = null;
private int xDragImageOffset = 0;
private int yDragImageOffset = 0;
private int xDragTouchOffset = 0;
private int yDragTouchOffset = 0;
private Context c;
public int selectedIndex = -1;
private final Bitmap bitMap;
private View view = null;
private boolean isPinch = false;
public SitesOverlay(Drawable marker, MapView mapView) {
super(boundCenter(marker));
this.marker = marker;
c = mapView.getContext();
dragImage = (ImageView) findViewById(R.id.drag);
xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();
items.add(new OverlayItem(getPoint(40.748963847316034,
-73.96807193756104), "UN", "United Nations"));
items.add(new OverlayItem(getPoint(40.76866299974387,
-73.98268461227417), "Lincoln Center",
"Home of Jazz at Lincoln Center"));
items.add(new OverlayItem(getPoint(40.765136435316755,
-73.97989511489868), "Carnegie Hall",
"Where you go with practice, practice, practice"));
items.add(new OverlayItem(getPoint(40.70686417491799,
-74.01572942733765), "The Downtown Club",
"Original home of the Heisman Trophy"));
bitMap = BitmapFactory.decodeResource(getResources(),
R.drawable.mekr);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
selectedIndex = -1;
}
@Override
protected boolean onTap(final int index) {
if ( isPinch ){
return false;
}else{
// getMapView().setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View arg0, MotionEvent arg1) {
// if (!items.isEmpty()) {
// if (view != null) {
// view.setVisibility(View.GONE);
// getMapView().invalidate();
// }
// }
// getMapView().invalidate();
// return true;
// }
// });
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
flag = false;
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
if (items.get(index).getTitle() != null
&& items.get(index).getTitle().equals("Me") == false) {
image.setImageResource(R.drawable.mekr);
}
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -bitMap.getHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
}
}
getMapView().invalidate();
}
});
selectedIndex = index;
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
if (action == MotionEvent.ACTION_DOWN) {
for (OverlayItem item : items) {
Point p = new Point(0, 0);
map.getProjection().toPixels(item.getPoint(), p);
if (hitTest(item, marker, x - p.x, y - p.y)) {
result = true;
inDrag = item;
items.remove(inDrag);
populate();
xDragTouchOffset = 0;
yDragTouchOffset = 0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;
isPinch=false;
break;
}
}
} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
if (view != null) {
if (view.getVisibility() != 0) {
Log.e("touch", " out move");
setDragImagePosition(x, y);
result = true;
isPinch=true;
}else{
setDragImagePosition(x, y);
isPinch=false;
}
}else{
setDragImagePosition(x, y);
result = true;
isPinch=true;
}
} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
dragImage.setVisibility(View.GONE);
GeoPoint pt = map.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
Log.e("touch", " out last" + pt.getLatitudeE6());
items.add(toDrop);
populate();
inDrag = null;
result = true;
isPinch = false;
}
return (isPinch || super.onTouchEvent(event,mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage.getLayoutParams();
lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
- yDragImageOffset - yDragTouchOffset, 0, 0);
dragImage.setLayoutParams(lp);
}
}
}
Here as shown in image that first one in which user can drag and drop pin.
here when the pop is shown that time user cant drag and drop pin.
No comments:
Post a Comment