Map marker dragging…

While working with Google map in android I found that dragging marker on the map is not a simple task. I found few examples which works well but still it looks some kind of messy. Finally I got one better solution on it which is like this drag map & keep marker fixed at the center of map in xml (In layout design). In Ontouch event we can get map’s center co-ordinates & as marker is centered on mapview it is pointing to the center co-ordinates of the map.
This way we can get marker pointing co-ordinates & also dragging is very smooth.

This is little difficult to understand but if you look at the code below you can get the idea.

XML file layout for map

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:layout_width="fill_parent"
	  android:layout_height="fill_parent">
		  <com.google.android.maps.MapView
		      	android:id="@+id/map"
		    	android:layout_width ="fill_parent"
		    	android:layout_height ="fill_parent"
			    android:apiKey="@string/map_api_key"
			    android:clickable="true"
		  />
		 <ImageView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:layout_centerInParent="true"
		        android:id="@+id/drag"
	    		android:src="@drawable/passenger_icon_black"
	    	/>
</RelativeLayout>

You activity will be like below

import java.text.DecimalFormat;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

public class MapDragActivity extends MapActivity implements LocationListener{
	String pinadd="";
	private MapView map=null;
	private LocationManager locationManager;
	GeoPoint myLocation;

	/** Called when the activity is first created. */

    @SuppressWarnings("static-access")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplayout);

        map=(MapView)findViewById(R.id.map);
	    map.setBuiltInZoomControls(true);

	    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

		if (location != null)
			plotLocation(location);
		else
			locationManager.requestLocationUpdates(
					locationManager.NETWORK_PROVIDER, 500L, 250.0f, this);

	}

	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null)
			plotLocation(location);
	}

	public void onProviderDisabled(String provider) {
	}

	public void onProviderEnabled(String provider) {
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	@SuppressWarnings("static-access")
	@Override
	public void onResume() {
		    super.onResume();
		    locationManager.requestLocationUpdates(
					locationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
   }

	@Override
	public void onPause() {
		    super.onPause();
		    locationManager.removeUpdates(this);
	}

	@Override
	public void onDestroy(){
		locationManager.removeUpdates(this);
		super.onDestroy();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event){
	    boolean result = super.dispatchTouchEvent(event);
	    if (event.getAction() == MotionEvent.ACTION_UP){
    	        	Projection projection = map.getProjection();
		        	int y = map.getHeight() / 2;
		        	int x = map.getWidth() / 2;

		        	GeoPoint geoPoint = projection.fromPixels(x, y);
		        	Log.v("Latitude & Logitude", geoPoint.getLatitudeE6() +"//"+geoPoint.getLongitudeE6());
	    }
	    return result;
	}

	double roundTwoDecimals(double d){
		    DecimalFormat twoDForm = new DecimalFormat("#.######");
		    return Double.valueOf(twoDForm.format(d));
	}

	public void plotLocation(Location location) {
			GeoPoint point = new GeoPoint(
					(int) (roundTwoDecimals(location.getLatitude()) * 1E6),
					(int) (roundTwoDecimals(location.getLongitude()) * 1E6));
			myLocation = point;
			map.getController().animateTo(point);
			map.getController().setCenter(point);
			zoomToMyLocation();
	}

	private void zoomToMyLocation() {
		if (myLocation != null) {
			map.getController().setZoom(18);
			map.getController().animateTo(myLocation);
		}
	}

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

}
About these ads

3 thoughts on “Map marker dragging…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s