how can i implement my own double-tap in mapview? - android

I have been working on an offline map using OSMdroid
I have map tiles of two zoom levels namely 12 and 15
Now the usual double tap zooms the mapview by 1 level
What i m trying to do is to setZoomLevel() as 15 after the user double taps on the map,
Is it possible?
I tried using onGestureListener , but somehow it is not working
Any hint or clues in that direction or sample codes would be a big help , Thanks

I had a similar requirement with the Osmdroid MapView, in that I didn't want it to do the 'centre on the double tapped location and zoom in' default functionality. I wanted it to pop up a Toast. In my case I had an overlay on top of the MapView, so I just had the overlay consume the double tap in its onDoubleTap method. For your purposes you could just add an overlay which draws nothing but has its own double tap functionality.
So at the end of your onCreate, you could add the overlay. This little app seems to demonstrate what you want - (you'll need to add conditional code for checking zoom level and other tinkering):
public class OsmdroidDemoMap extends Activity {
private MapView mMapView;
private MapController mMapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.osm_main);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapController = mMapView.getController();
mMapController.setZoom(13);
GeoPoint gPt = new GeoPoint(51500000, -150000);
mMapController.setCenter(gPt);
DummyOverlay dumOverlay = new DummyOverlay(this);
List<Overlay> listOfOverlays = mMapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(dumOverlay);
}
public class DummyOverlay extends org.osmdroid.views.overlay.Overlay {
public DummyOverlay(Context ctx) {
super(ctx); // TODO Auto-generated constructor stub
}
#Override
protected void draw(Canvas c, MapView osmv, boolean shadow) {}
#Override
public boolean onDoubleTap(MotionEvent e, MapView mapView) {
// This stops the 'jump to, and zoom in' of the default behaviour
int zoomLevel = mMapView.getZoomLevel();
mMapController.setZoom(zoomLevel + 3);
return true;// This stops the double tap being passed on to the mapview
}
}

If you need to manage the double-tap in an MapView, i suggest you to check this thread:
android maps: How to Long Click a Map?
You can see my answer and how i adapted mapview-overlay-manager.

you can override the zoom control with its event, for that you need to define into the xml layout or you get the zoom control from the mapview
ZoomControls zoomControls = (ZoomControls)findViewById(R.id.zoomControl);
View zoomOut = zoomControls.getChildAt(0);
zoomOut.setBackgroundDrawable(getResources().getDrawable(R.drawable.zoom_out_icon));
zoomOut.setPadding(5, 5, 5, 5);
View zoomIn = zoomControls.getChildAt(1);
zoomIn.setBackgroundDrawable(getResources().getDrawable(R.drawable.zoom_in_icon));
zoomIn.setPadding(5, 5, 5, 5);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomIn();
mapView.invalidate();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomOut();
mapView.invalidate();
}
});
now you can implement your own zoom control

In the latest OSMdroid library 4.0 has inbuilt automatic double tap zoom in. But nothing is better than having a zoom control.

you can use just add below line
mMapView.setMultiTouchControls(true);

Related

How do I change the result of mylocationoverlay?

I am using mylocationoverlay to give my current location, however, its not exactly accurate. Sometimes it is up to 20 meters wrong. I want to be able to move the location marker to my exact position. When using the following code, when I press on the marker, the map moves and the marker stays in the same place. How do I keep the map static and allow a user to move the marker?
public class selectmap extends MapActivity {
private MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectmap);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
mapView.getController().setZoom(17);
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
}
}
You have to write your own implementation to handle this scenario. Here is an example application where you can move the markers
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/NooYawkTouch/

What is this little strange circle in the middle of the map

I am developing with Google Maps Add-on. I found a little strange circle in the middle of my MapView. When I scroll the view or tap somewhere else, it's gone.
I debug the view with Hierarchy View tools, you can see is from inside of MapView, definitely not some misplace view from my code.
The red marker is from the overlay, just simply pin on the center of this MapView.
Here is the code, pretty standard MapView code:
mMapView = new MapView(getActivity(), R.string.key_mapAPIKey);
mMapView.setClickable(false);
mMapView.getController().setZoom(DEFAULT_MAP_ZOOM);
mMapView.getController().animateTo(geoPoint);
mMapView.getOverlays().clear();
mMapView.getOverlays().add(new GoogleMapClassicMarkerOverlay(getResources(), geoPoint));
UPDATE-10/30
The Overlay is also ordinary, just to show a single marker in the middle, I remove the overlay but the circle is still there:
public class GoogleMapClassicMarkerOverlay extends ItemizedOverlay<OverlayItem> {
private GeoPoint mGeoPoint;
private OverlayItem mItem;
public GoogleMapClassicMarkerOverlay(Resources res, GeoPoint geoPoint) {
super(boundCenterBottom(new SafeBitmapDrawable(res, R.drawable.map_marker)));
mGeoPoint = geoPoint;
mItem = new OverlayItem(mGeoPoint, "", "");
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mItem;
}
#Override
public int size() {
return 1;
}
}
Anyone has idea how to remove this wired circle?

How to create a static Mapview that recognize an onTap method

I'm trying to develop a layout that shows a Google Map with a route drawn on a Overlay layer associated with this map. I want this mapView to be a small static map that shows, as a thumbnail, the main route the user followed, and then if the user clicks on it, an intent takes you to a different activity displaying the map in full screen with the route and all the zoom functionalities.
The thing is that although I override the onTap method of the Overlay setting there the intent to the new activity, it only works if I set the MapView as setEnabled(true), but if I do so, then the thumbnail map can be dragged and moved by the user.
I'm sorry if it is not clear enough, but I don't know how to explain it better.
Thanks in advance
This is my customized class which extends Overlay and overrides the onTap method:
class MapOverlay extends Overlay {
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
Intent i = new Intent(getApplicationContext(),
RouteMapActivity.class);
startActivity(i);
return false;
};
And this is my onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailrun_activity);
map = (MapView) findViewById(R.id.mvMain);
map.setEnabled(true);
map.setClickable(true);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = map.getOverlays();
projection = map.getProjection();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
double lat = 28.063119 * 1E6, longi = -82.41128 * 1E6;
GeoPoint point = new GeoPoint((int) lat, (int) longi);
MapController myMapController = map.getController();
myMapController.setCenter(point);
}
For now I'm only drawing a straight line between two GeoPoints, and I don't get any logcat errors.
The map with the route is indeed drawn, and the overrided onTap method works, but it seems it only recognizes the tap if my mapView is defined as enabled and clickable, but if I do so, then the user can also move and drag the map as long as he holds pressing the screen.
you can get static google map image as per your location by this link. that you can display in imageview and click on that you can target new activity which have MapView.. i think it may be a good option for you.

Android MapView MapController stopAnimation not stopping animation

well, i'm developing a geolocation app in Android. On first run we center the map at the current location, then the user can zoom and pan freedomly, but we have a button that animates the map and centers it back to the actual position.
The problem is that this just happens when the map is static: if the user scrolls the map and leaves it scrolling by inertia, this button won't work until the animation is stopped.
Here's the code.
mapView.getController().stopAnimation(false); //this aint working as expected
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
Thanks.
This works for me:
public void centerCurrentClickHandler(View v) {
if (hasCurrentPosition) {
GeoPoint point = new GeoPoint(currentLatitudeE6, currentLongitudeE6);
mapController.animateTo(point);
}
}
public void centerFlagClickHandler(View v) {
if (hasPushpinPosition) {
GeoPoint point = new GeoPoint(pushpinLatitudeE6, pushpinLongitudeE6);
mapController.animateTo(point);
}
}

Porting a Google Maps app to Osmdroid - problem with overlay

I'm porting a Google Maps based project to Osmdroid in order to use OpenStreetMaps. The port is working OK apart from adding my overlay which consists of a number of straight lines and some text. In both projects I add the Overlay by means of a timer thread and handler calling redrawOverlay.
In the OSM project my overlay is just a grey square completely hiding the map. If I remove the call to redrawOveraly, the OSM tiles are shown OK. I've reduced the overlay code to the bare minimum of a single diagonal line in the code samples below. It works fine in the Google app, overlaying the map tile. The com.google.android.maps.Overlay has a draw method, the OSM has an onDraw, so I have in the OSM version:
private MapView mv;
private MapOverlay mmapOverlay = null;
private void redrawOverlay() {
gPt = mv.getMapCenter();
if (mmapOverlay == null)
mmapOverlay = new MapOverlay(getApplicationContext());
List<Overlay> listOfOverlays = mv.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mmapOverlay);
mv.invalidate();
}
public class MapOverlay extends org.osmdroid.views.overlay.Overlay {
public MapOverlay(Context ctx) {
super(ctx);
// TODO Auto-generated constructor stub
}
#Override
public void onDraw(Canvas canvas, MapView mapView) {
Paint lp3;
lp3 = new Paint();
lp3.setColor(Color.RED);
lp3.setAntiAlias(true);
lp3.setStyle(Style.STROKE);
lp3.setStrokeWidth(1);
lp3.setTextAlign(Paint.Align.LEFT);
lp3.setTextSize(12);
canvas.drawLine(10, 10, 150, 150, lp3);
}
Whilst in the Google maps original I have the equivalent :
public class MapOverlay extends com.google.android.maps.Overlay {
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint lp3;
lp3 = new Paint();
.....etc.
redrawOverlay is the same except the instantiation of the overlay is just:
mmapOverlay = new MapOverlay();
All suggestions will be gratefully received
UPDATE Question for kurtzmarc:
Thanks for you help so far, I see that you are one of the authors of Osmdroid. I like what it's doing so far. I would like to suppress the 'jump to and zoom in' that you get on double tap. I'd like it to do nothing at all. I think it's probably hitting this bit in your source and doing the zoomInFixing:
private class MapViewDoubleClickListener implements GestureDetector.OnDoubleTapListener {
#Override
public boolean onDoubleTap(final MotionEvent e) {
for (int i = mOverlays.size() - 1; i >= 0; i--)
if (mOverlays.get(i).onDoubleTapUp(e, MapView.this))
return true;
final GeoPoint center = getProjection().fromPixels(e.getX(), e.getY());
return zoomInFixing(center);
}
It doesn't seem that I can override it. I'm using the 3.0.1 jar and the associated javadocs. I'm wondering if the Mapview's setTouchDelegate method would help, but there is no reference to it in the javadocs. Have you any suggestions please?
I'm not sure where you are calling redrawOverlay() from, but if you look at the MinimapOverlay you will see an example where something is drawn at a fixed location on the screen. In other words, you are drawing in screen coordinates not in map coordinates.
Example:
#Override
protected void onDraw(final Canvas pC, final MapView pOsmv) {
// Calculate the half-world size
final Rect viewportRect = new Rect();
final Projection projection = pOsmv.getProjection();
final int zoomLevel = projection.getZoomLevel();
final int tileZoom = projection.getTileMapZoom();
mWorldSize_2 = 1 << (zoomLevel + tileZoom - 1);
// Find what's on the screen
final BoundingBoxE6 boundingBox = projection.getBoundingBox();
final Point upperLeft = org.osmdroid.views.util.Mercator
.projectGeoPoint(boundingBox.getLatNorthE6(), boundingBox.getLonWestE6(),
zoomLevel + tileZoom, null);
final Point lowerRight = org.osmdroid.views.util.Mercator
.projectGeoPoint(boundingBox.getLatSouthE6(), boundingBox.getLonEastE6(), zoomLevel
+ tileZoom, null);
// Save the Mercator coordinates of what is on the screen
viewportRect.set(upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);
// Offset into OSM coordinates
viewportRect.offset(-mWorldSize_2, -mWorldSize_2);
// Draw a line from one corner to the other
canvas.drawLine(viewportRect.left, viewportRect.top, viewportRect.right, viewportRect.bottom);
From here viewportRect represents the upper left to the lower right of the screen. You can use this to draw at any fixed points on the screen.
UPDATE:
To answer your second question - what you need to do is override onDoubleTap in your Overlay and return "true". Returning "true" indicates to the base class that you "consumed" the event and no further processing should take place. Take a look at the minimap overlay code for a good example:
http://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-android/src/org/osmdroid/views/overlay/MinimapOverlay.java
We are right in the middle of overhauling the Overlays, so some of this will be handled a little better in the near future. For example, the getOverlays().clear() bug you ran into has also been reported elsewhere and we've since fixed it.

Categories

Resources