package lee.hyeontae;

 

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

 

import android.os.Bundle;

 

public class MaptestActivity extends MapActivity

{

       // Members

      Maptest mMapView;

 

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

 

        // Populate the map member

        mMapView = (Maptest) findViewById(R.id.mapview);

 

        // Add zoom controls

        mMapView.setBuiltInZoomControls(true);

 

        // Add listener

        mMapView.setOnChangeListener(new MapViewChangeListener());

    }

 

    private class MapViewChangeListener implements Maptest.OnChangeListener

    {

 

        @Override

        public void onChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter, int newZoom, int oldZoom)

        {

            // Check values

            if ((!newCenter.equals(oldCenter)) && (newZoom != oldZoom))

            {

                // Map Zoom and Pan Detected

                // TODO: Add special action here

            }

            else if (!newCenter.equals(oldCenter))

            {

                // Map Pan Detected

                // TODO: Add special action here

            }

            else if (newZoom != oldZoom)

            {

                // Map Zoom Detected

                // TODO: Add special action here

            }

        }

    }

   

   

      @Override

      protected boolean isRouteDisplayed()

      {

            return false;

      }

 

}

 


 

package lee.hyeontae;

 

import java.util.Timer;

import java.util.TimerTask;

 

import android.content.Context;

import android.util.AttributeSet;

import android.view.MotionEvent;

 

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapView;

 

public class Maptest extends MapView

{

    // ------------------------------------------------------------------------

    // LISTENER DEFINITIONS

    // ------------------------------------------------------------------------

 

    // Change listener

    public interface OnChangeListener

    {

        public void onChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter, int newZoom, int oldZoom);

    }

 

    // ------------------------------------------------------------------------

    // MEMBERS

    // ------------------------------------------------------------------------

 

    private Maptest mThis;

    private long mEventsTimeout = 250L;     // Set this variable to your preferred timeout

    private boolean mIsTouched = false;

    private GeoPoint mLastCenterPosition;

    private int mLastZoomLevel;

    private Timer mChangeDelayTimer = new Timer();

    private Maptest.OnChangeListener mChangeListener = null;

 

    // ------------------------------------------------------------------------

    // CONSTRUCTORS

    // ------------------------------------------------------------------------

 

    public Maptest(Context context, String apiKey)

    {

        super(context, apiKey);

        init();

    }

 

    public Maptest(Context context, AttributeSet attrs)

    {

        super(context, attrs);

        init();

    }

 

    public Maptest(Context context, AttributeSet attrs, int defStyle)

    {

        super(context, attrs, defStyle);

        init();

    }

 

    private void init()

    {

        mThis = this;

        mLastCenterPosition = this.getMapCenter();

        mLastZoomLevel = this.getZoomLevel();

    }

 

    // ------------------------------------------------------------------------

    // GETTERS / SETTERS

    // ------------------------------------------------------------------------

 

    public void setOnChangeListener(Maptest.OnChangeListener l)

    {

        mChangeListener = l;

    }

 

    // ------------------------------------------------------------------------

    // EVENT HANDLERS

    // ------------------------------------------------------------------------

 

    @Override

    public boolean onTouchEvent(MotionEvent ev)

    {

        // Set touch internal

        mIsTouched = (ev.getAction() != MotionEvent.ACTION_UP);

 

        return super.onTouchEvent(ev);

    }

 

    @Override

    public void computeScroll()

    {

        super.computeScroll();

 

        // Check for change

        if (isSpanChange() || isZoomChange())

        {

            // If computeScroll called before timer counts down we should drop it and

            // start counter over again

            resetMapChangeTimer();

        }

    }

 

    // ------------------------------------------------------------------------

    // TIMER RESETS

    // ------------------------------------------------------------------------

 

    private void resetMapChangeTimer()

    {

        mChangeDelayTimer.cancel();

        mChangeDelayTimer = new Timer();

        mChangeDelayTimer.schedule(new TimerTask()

        {

            @Override

            public void run()

            {

                if (mChangeListener != null) mChangeListener.onChange(mThis, getMapCenter(), mLastCenterPosition, getZoomLevel(), mLastZoomLevel);

                mLastCenterPosition = getMapCenter();

                mLastZoomLevel = getZoomLevel();

            }

        }, mEventsTimeout);

    }

 

    // ------------------------------------------------------------------------

    // CHANGE FUNCTIONS

    // ------------------------------------------------------------------------

 

    private boolean isSpanChange()

    {

        return !mIsTouched && !getMapCenter().equals(mLastCenterPosition);

    }

 

    private boolean isZoomChange()

    {

        return (getZoomLevel() != mLastZoomLevel);

    }

 

}



 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <lee.hyeontae.Maptest

        android:id="@+id/mapview"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:apiKey="0e4Cy7pA6hkSrVYuH5139XIFCKMejJRMIBoKj1w"

        android:clickable="true" />

 

</LinearLayout>






http://bricolsoftconsulting.com/2011/10/31/extending-mapview-to-add-a-change-event/

신고
Posted by DoDo4989

댓글을 달아 주세요