Tuesday, December 8, 2009

Move the marker on MapView

It's a bug here:

Using onTouchEvent(MotionEvent, MapView) to handle user touching as in the example, it's work fine on Android Emulator, but not working on true phone.

Please refer to another article "Problem of onTouchEvent(MotionEvent, MapView) on MapView" for details.

Thanks thibault let me know the case:)

Android Er@2010-06-22

In the last exercises, Display a marker on MapView, using Overlays and Get center location of a map, using MapView.getMapCenter(), a marker is displayed on the center of the MapView. In this exercise, I want to add the capability of moving the marker on the Map. Unfortunately, I can't find any solution (from books and internet) to move (drag) the marker on MapView, similar to web version. Finally, I made a alternative.

If user Touch on screen without moving, it will be treated as update marker, the marker will be place on the new location, and it will be centered on the MapView. If user Touch on screen and Move, it will be treated as pan the MapView, the marker will not be changed.

The main logic is in the method onTouchEvent(MotionEvent arg0, MapView arg1), ACTION_UP, ACTION_DOWN and ACTION_MOVE cases.

((int)arg0.getX(), (int)arg0.getY()) is the on-screen pixel coordinates, it can be translated to latitude/longitude(GePoint) using Projection.fromPixels.


package com.AndroidMapper;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class AndroidMapView extends MapActivity {

private TextView myLongitude, myLatitude;
private CheckBox mySatellite;

private MapView myMapView;
private MapController myMapController;

private void SetSatellite()

protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub

Bundle bundle = this.getIntent().getExtras();
int Mode = bundle.getInt("Mode");

myMapView = (MapView)findViewById(R.id.mapview);
myMapController = myMapView.getController();

myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
mySatellite = (CheckBox)findViewById(R.id.satellite);


if(Mode == 0)
GeoPoint initGeoPoint = myMapView.getMapCenter();
else if(Mode == 1)
int intLatitude = bundle.getInt("Latitude");
int intLongitude = bundle.getInt("Longitude");
GeoPoint initGeoPoint = new GeoPoint(intLatitude, intLongitude);

private void placeMarker(int markerLatitude, int markerLongitude)
Drawable marker=getResources().getDrawable(
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
myMapView.getOverlays().add(new InterestingLocations(marker,
markerLatitude, markerLongitude));

protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;

private void CenterLocation(GeoPoint centerGeoPoint)

myLongitude.setText("Longitude: "+
myLatitude.setText("Latitude: "+

private CheckBox.OnClickListener mySatelliteOnClickListener =
new CheckBox.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub

class InterestingLocations extends ItemizedOverlay<OverlayItem>{

private List<OverlayItem> locations =
new ArrayList<OverlayItem>();
private Drawable marker;
private OverlayItem myOverlayItem;

boolean MoveMap;

public InterestingLocations(Drawable defaultMarker,
int LatitudeE6, int LongitudeE6) {
// TODO Auto-generated constructor stub
// create locations of interest
GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6);
myOverlayItem = new OverlayItem(myPlace, "My Place", "My Place");


protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return locations.get(i);

public int size() {
// TODO Auto-generated method stub
return locations.size();

public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);


public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {
// TODO Auto-generated method stub
//super.onTouchEvent(arg0, arg1);

int Action = arg0.getAction();
if (Action == MotionEvent.ACTION_UP){

Projection proj = myMapView.getProjection();
GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());

//remove the last marker


else if (Action == MotionEvent.ACTION_DOWN){

MoveMap = false;

else if (Action == MotionEvent.ACTION_MOVE){
MoveMap = true;

return super.onTouchEvent(arg0, arg1);
//return false;

Download the files.


thibault said...

Hi I got a probleme moving my overlay. Actually i can move it on my emulator android but no on my phone... I gonna be crazy trying to make it worked.is someone got an idea for my problem?

thibault said...
This comment has been removed by the author.
Android Er said...

I'm not sure- but in my memory Map API have some change in 1.5 (http://developer.android.com/guide/tutorials/views/hello-mapview.html). Do G1 run 1.1? May be it's the problem.

NickTack said...

Isn't it easier to override the MapOverlay's onTap() method with this code?

super.onTap(p, mapView);
drawingPoint = p;
mapView.invalidate(); mapView.getController().animateTo(p);
return false;

SAS said...

I have also tried using onTap().
There are some problems in using onTap().
For example, if setBuiltInZoomControls is true, when you zoom in or out by two fingers, it will change the location of marker.
However, using the method in this post have no this problem.
Maybe it have other better solution.

Yao said...

Hi, may I know why my radio button for the GPS is not clickable? the other two work fine.

Andr.oid Eric said...


As I remember: are you run on emulator? if there are no valid location, the GPS selection will be disabled.