<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>MapView on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/mapview/</link>
    <description>Recent content in MapView on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 15 Dec 2011 18:29:51 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/mapview/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Android Map View With Pan &amp; Zoom Events</title>
      <link>https://mpcabd.xyz/android-map-view-with-pan-zoom-events/</link>
      <pubDate>Thu, 15 Dec 2011 18:29:51 +0000</pubDate>
      <guid>https://mpcabd.xyz/android-map-view-with-pan-zoom-events/</guid>
      <description>&lt;p&gt;I was working an Android application, and I had an activity that contains a map in it, the map is hosted inside a &lt;a href=&#34;http://developer.android.com/resources/tutorials/views/hello-mapview.html&#34;&gt;MapView&lt;/a&gt;. The application had to show some pins over the map, the pins locations are obtained from a web service. I had to retrieve the pins locations after the user pans the map or zooms in or out.&lt;/p&gt;
&lt;p&gt;Google didn’t provide events for panning and zooming inside MapView class, but you can extend the class and do it your self, and that’s what I did.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I was working an Android application, and I had an activity that contains a map in it, the map is hosted inside a <a href="http://developer.android.com/resources/tutorials/views/hello-mapview.html">MapView</a>. The application had to show some pins over the map, the pins locations are obtained from a web service. I had to retrieve the pins locations after the user pans the map or zooms in or out.</p>
<p>Google didn’t provide events for panning and zooming inside MapView class, but you can extend the class and do it your self, and that’s what I did.</p>
<p><a href="http://mpcabd.xyz/wp-content/uploads/2011/12/Androids.png" rel="lightbox[320]" title="Androids"><img src="http://mpcabd.xyz/wp-content/uploads/2011/12/Androids-300x105.png" alt="" title="Androids" width="300" height="105" class="aligncenter size-medium wp-image-315" srcset="https://mpcabd.xyz/wp-content/uploads/2011/12/Androids-300x105.png 300w, https://mpcabd.xyz/wp-content/uploads/2011/12/Androids.png 766w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<h3 id="listener-interface">Listener Interface</h3>
<pre class="lang:java decode:true">import com.google.android.maps.GeoPoint;

public interface MapViewListener {
	void onPan(GeoPoint oldTopLeft,
			   GeoPoint oldCenter,
			   GeoPoint oldBottomRight,
			   GeoPoint newTopLeft,
			   GeoPoint newCenter,
			   GeoPoint newBottomRight);
	void onZoom(GeoPoint oldTopLeft,
				GeoPoint oldCenter,
				GeoPoint oldBottomRight,
				GeoPoint newTopLeft,
				GeoPoint newCenter,
				GeoPoint newBottomRight,
				int oldZoomLevel,
				int newZoomLevel);
	void onClick(GeoPoint clickedPoint);
}
</pre>
<p>This listener will receive three events, onPan when the user pans the map, onZoom when the user zooms in or out, onClick when the user taps on the map without panning.</p>
<p>Each event will pass useful parameters, which are old and new values for the top left corner, bottom right corner, center point and zoom level.</p>
<h3 id="map-view-code">Map View Code</h3>
<p>The new map view will inherit the MapView class, and define some useful variables:</p>
<pre class="lang:java decode:true">import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class ObservableMapView extends MapView {
	public ObservableMapView(Context context, String apiKey) { super(context, apiKey); }
	public ObservableMapView(Context context, AttributeSet attrs) { super(context, attrs); }
	public ObservableMapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
	
	private GeoPoint mOldTopLeft;
	private GeoPoint mOldCenter;
	private GeoPoint mOldBottomRight;
	private int mOldZoomLevel = -1;
	
	private MapViewListener mMapViewListener;
	public MapViewListener getMapViewListener() { return mMapViewListener; }
	public void setMapViewListener(MapViewListener value) { mMapViewListener = value; }

// ...
}
</pre>
<p>To get touch events we’re going to override the onTouchEvent:</p>
<pre class="lang:java decode:true">@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_UP) {
			GeoPoint newCenter = this.getMapCenter();
			GeoPoint newTopLeft = this.getProjection().fromPixels(0, 0);
			GeoPoint newBottomRight = this.getProjection().fromPixels(this.getWidth(), this.getHeight());
			
			if (this.mMapViewListener != null &&
				newTopLeft.getLatitudeE6() == mOldTopLeft.getLatitudeE6() &&
				newTopLeft.getLongitudeE6() == mOldTopLeft.getLongitudeE6()) {
				mMapViewListener.onClick(this.getProjection().fromPixels((int)ev.getX(), (int)ev.getY()));
			}
		}
		return super.onTouchEvent(ev);
	}
</pre>
<p>Here we’re checking the new coordinates with the old coordinates, if they haven’t changed then the user has tapped on the map, but if they changed then it is a panning event, we’re not going to emit the event of panning now.</p>
<p>To be able to find if the user has zoomed we’re going to override the dispatchDraw method, because it will be called when the user pans or zooms, so it is the place we’re going to check for events in it.</p>
<pre class="lang:java decode:true">@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		
		GeoPoint newCenter = this.getMapCenter();
		GeoPoint newTopLeft = this.getProjection().fromPixels(0, 0);
		GeoPoint newBottomRight = this.getProjection().fromPixels(this.getWidth(), this.getHeight());
		int newZoomLevel = this.getZoomLevel();
		
		if (mOldCenter == null)
			mOldCenter = newCenter;
		
		if (mOldTopLeft == null)
			mOldTopLeft = newTopLeft;

		if (mOldBottomRight == null)
			mOldBottomRight = newBottomRight;

		if (newTopLeft.getLatitudeE6() != mOldTopLeft.getLatitudeE6() || newTopLeft.getLongitudeE6() != mOldTopLeft.getLongitudeE6()) {
			if (this.mMapViewListener != null) {
				GeoPoint oldTopLeft, oldCenter, oldBottomRight;
				
				oldTopLeft = mOldTopLeft;
				oldCenter = mOldCenter;
				oldBottomRight = mOldBottomRight;
		
				mOldBottomRight = newBottomRight;
				mOldTopLeft = newTopLeft;
				mOldCenter = newCenter;
				
				mMapViewListener.onPan(oldTopLeft,
									   oldCenter,
									   oldBottomRight,
									   newTopLeft,
									   newCenter,
									   newBottomRight);
			}
		}
		
		if (mOldZoomLevel == -1)
			mOldZoomLevel = newZoomLevel;
		else if (mOldZoomLevel != newZoomLevel && mMapViewListener != null) {
			int oldZoomLevel = mOldZoomLevel;
			GeoPoint oldTopLeft, oldCenter, oldBottomRight;
			oldTopLeft = mOldTopLeft;
			oldCenter = mOldCenter;
			oldBottomRight = mOldBottomRight;
	
			mOldZoomLevel = newZoomLevel;
			mOldBottomRight = newBottomRight;
			mOldTopLeft = newTopLeft;
			mOldCenter = newCenter;
			
			mMapViewListener.onZoom(oldTopLeft,
									oldCenter,
									oldBottomRight,
									newTopLeft,
									newCenter,
									newBottomRight,
									oldZoomLevel,
									newZoomLevel);
		}
	}
</pre>
<h3 id="download">Download</h3>
<p>The source code is licensed under the <a href="http://www.gnu.org/copyleft/gpl.html">GNU Public License (GPL)</a>.</p>
<p>You can download the source code from here:</p>
<p><a href="https://github.com/mpcabd/android-map-view-pan-zoom">Project on GitHub</a></p>
<p><a href="https://github.com/mpcabd/android-map-view-pan-zoom/tarball/master">Source code from GitHub</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>
