Android Open Source - android_maplib Map Event Source






From Project

Back to project page android_maplib.

License

The source code is released under:

GNU General Public License

If you think the Android project android_maplib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Project:  NextGIS Mobile//www . java 2s  .  c  o m
 * Purpose:  Mobile GIS for Android.
 * Author:   Dmitry Baryshnikov (aka Bishop), bishop.dev@gmail.com
 * *****************************************************************************
 * Copyright (c) 2012-2015. NextGIS, info@nextgis.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nextgis.maplib.map;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.nextgis.maplib.api.ILayer;
import com.nextgis.maplib.api.MapEventListener;
import com.nextgis.maplib.datasource.GeoPoint;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MapEventSource
        extends MapContentProviderHelper
{
    protected static final String BUNDLE_ID_KEY        = "id";
    protected static final String BUNDLE_TYPE_KEY      = "type";
    protected static final String BUNDLE_DONE_KEY      = "done";
    protected static final String BUNDLE_ZOOM_KEY      = "zoom";
    protected static final String BUNDLE_X_KEY      = "x";
    protected static final String BUNDLE_Y_KEY      = "y";
    protected final static int EVENT_onLayerAdded = 1;
    protected final static int EVENT_onLayerDeleted = 2;
    protected final static int EVENT_onLayerChanged = 3;
    protected final static int EVENT_onExtentChanged = 4;
    protected final static int EVENT_onLayersReordered = 5;
    protected final static int EVENT_onLayerDrawFinished = 6;
    protected List<MapEventListener> mListeners;
    protected Handler                mHandler;


    public MapEventSource(
            Context context,
            File mapPath,
            LayerFactory layerFactory)
    {
        super(context, mapPath, layerFactory);
        mListeners = new ArrayList<>();

        createHandler();
    }


    /**
     * Add new listener for map events
     *
     * @param listener
     *         A listener class implements MapEventListener adding to listeners array
     */
    public void addListener(MapEventListener listener)
    {
        if (mListeners != null && !mListeners.contains(listener)) {
            mListeners.add(listener);
        }
    }


    /**
     * Remove listener from listeners
     *
     * @param listener
     *         A listener class implements MapEventListener removing from listeners array
     */
    public void removeListener(MapEventListener listener)
    {
        if (mListeners != null) {
            mListeners.remove(listener);
        }
    }


    @Override
    public void onDrawFinished(
            int id,
            float percent)
    {
        super.onDrawFinished(id, percent);
        onLayerDrawFinished(id, percent);
    }


    /**
     * Send layer added event to all listeners
     *
     * @param layer
     *         A new layer
     */
    @Override
    protected void onLayerAdded(ILayer layer)
    {
        super.onLayerAdded(layer);
        if (mListeners == null) {
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_ID_KEY, layer.getId());
        bundle.putInt(BUNDLE_TYPE_KEY, EVENT_onLayerAdded);

        Message msg = new Message();
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }


    /**
     * Send layer changed event to all listeners
     *
     * @param layer
     *         A changed layer
     */
    @Override
    protected void onLayerChanged(ILayer layer)
    {
        super.onLayerChanged(layer);
        if (mListeners == null) {
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_ID_KEY, layer.getId());
        bundle.putInt(BUNDLE_TYPE_KEY, EVENT_onLayerChanged);

        Message msg = new Message();
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }


    /**
     * Send layer delete event to all listeners
     *
     * @param id
     *         A deleted layer identificator
     */
    @Override
    protected void onLayerDeleted(int id)
    {
        super.onLayerDeleted(id);
        if (mListeners == null) {
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_ID_KEY, id);
        bundle.putInt(BUNDLE_TYPE_KEY, EVENT_onLayerDeleted);

        Message msg = new Message();
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }


    /**
     * Send extent change event to all listeners
     *
     * @param zoom
     *         A zoom level
     * @param center
     *         A map center coordinates
     */
    @Override
    protected void onExtentChanged(
            float zoom,
            GeoPoint center)
    {
        super.onExtentChanged(zoom, center);
        if (mListeners == null) {
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putFloat(BUNDLE_ZOOM_KEY, zoom);
        bundle.putDouble(BUNDLE_X_KEY, center.getX());
        bundle.putDouble(BUNDLE_Y_KEY, center.getY());
        bundle.putInt(BUNDLE_TYPE_KEY, EVENT_onExtentChanged);

        Message msg = new Message();
        msg.setData(bundle);
        mHandler.sendMessage(msg);

    }


    /**
     * Send layers reordered event to all listeners
     */
    @Override
    protected void onLayersReordered()
    {
        super.onLayersReordered();
        if (mListeners == null) {
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_TYPE_KEY, EVENT_onLayersReordered);

        Message msg = new Message();
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }


    /**
     * Send layers draw finished event to all listeners
     */
    protected void onLayerDrawFinished(
            int id,
            float percent)
    {
        if (mListeners == null) {
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_TYPE_KEY, EVENT_onLayerDrawFinished);
        bundle.putInt(BUNDLE_ID_KEY, id);
        bundle.putFloat(BUNDLE_DONE_KEY, percent);

        Message msg = new Message();
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

    /**
     * Create handler for messages
     */
    protected void createHandler(){
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                Bundle resultData = msg.getData();

                for (MapEventListener listener : mListeners) {
                    switch(resultData.getInt(BUNDLE_TYPE_KEY)) {
                        case EVENT_onLayerAdded:
                            listener.onLayerAdded(resultData.getInt(BUNDLE_ID_KEY));
                            break;
                        case EVENT_onLayerDeleted:
                            listener.onLayerDeleted(resultData.getInt(BUNDLE_ID_KEY));
                            break;
                        case EVENT_onLayerChanged:
                            listener.onLayerChanged(resultData.getInt(BUNDLE_ID_KEY));
                            break;
                        case EVENT_onExtentChanged:
                            listener.onExtentChanged(resultData.getFloat(BUNDLE_ZOOM_KEY), new GeoPoint(resultData.getDouble(BUNDLE_X_KEY), resultData.getDouble(BUNDLE_Y_KEY)));
                            break;
                        case EVENT_onLayerDrawFinished:
                            listener.onLayerDrawFinished(resultData.getInt(BUNDLE_ID_KEY), resultData.getFloat(BUNDLE_DONE_KEY));
                            break;
                        case EVENT_onLayersReordered:
                            listener.onLayersReordered();
                            break;
                    }
                }
            }
        };
    }
}




Java Source Code List

com.nextgis.maplib.ApplicationTest.java
com.nextgis.maplib.api.GpsEventListener.java
com.nextgis.maplib.api.IGISApplication.java
com.nextgis.maplib.api.IJSONStore.java
com.nextgis.maplib.api.ILayerView.java
com.nextgis.maplib.api.ILayer.java
com.nextgis.maplib.api.IMapView.java
com.nextgis.maplib.api.INGWLayer.java
com.nextgis.maplib.api.IRenderer.java
com.nextgis.maplib.api.MapEventListener.java
com.nextgis.maplib.datasource.DatabaseHelper.java
com.nextgis.maplib.datasource.GeoEnvelope.java
com.nextgis.maplib.datasource.GeoGeometryCollection.java
com.nextgis.maplib.datasource.GeoGeometry.java
com.nextgis.maplib.datasource.GeoLineString.java
com.nextgis.maplib.datasource.GeoLinearRing.java
com.nextgis.maplib.datasource.GeoMultiLineString.java
com.nextgis.maplib.datasource.GeoMultiPoint.java
com.nextgis.maplib.datasource.GeoMultiPolygon.java
com.nextgis.maplib.datasource.GeoPoint.java
com.nextgis.maplib.datasource.GeoPolygon.java
com.nextgis.maplib.datasource.Geo.java
com.nextgis.maplib.datasource.NGWLayerContentProvider.java
com.nextgis.maplib.datasource.TileItem.java
com.nextgis.maplib.datasource.ngw.Connection.java
com.nextgis.maplib.datasource.ngw.Connections.java
com.nextgis.maplib.datasource.ngw.INGWResource.java
com.nextgis.maplib.datasource.ngw.LayerWithStyles.java
com.nextgis.maplib.datasource.ngw.ResourceGroup.java
com.nextgis.maplib.datasource.ngw.Resource.java
com.nextgis.maplib.display.GISDisplay.java
com.nextgis.maplib.display.Renderer.java
com.nextgis.maplib.display.SimpleFeatureRenderer.java
com.nextgis.maplib.display.SimpleLineStyle.java
com.nextgis.maplib.display.SimpleMarkerStyle.java
com.nextgis.maplib.display.Style.java
com.nextgis.maplib.display.TMSRenderer.java
com.nextgis.maplib.location.GpsEventSource.java
com.nextgis.maplib.map.LayerFactory.java
com.nextgis.maplib.map.LayerGroup.java
com.nextgis.maplib.map.Layer.java
com.nextgis.maplib.map.MapBase.java
com.nextgis.maplib.map.MapContentProviderHelper.java
com.nextgis.maplib.map.MapDrawable.java
com.nextgis.maplib.map.MapEventSource.java
com.nextgis.maplib.map.NGWRasterLayer.java
com.nextgis.maplib.map.NGWVectorLayer.java
com.nextgis.maplib.map.RemoteTMSLayer.java
com.nextgis.maplib.map.TMSLayer.java
com.nextgis.maplib.map.VectorLayer.java
com.nextgis.maplib.util.Constants.java
com.nextgis.maplib.util.DatabaseContext.java
com.nextgis.maplib.util.Feature.java
com.nextgis.maplib.util.FileUtil.java
com.nextgis.maplib.util.GeoConstants.java
com.nextgis.maplib.util.NetworkUtil.java
com.nextgis.maplib.util.SettingsConstants.java
com.nextgis.maplib.util.VectorCacheItem.java