GIS的学习(四十三)osmdroid基于几个经典代理类

   在osmdroid中给基于位置的代理类如下:

package org.osmdroid;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationListenerProxy implements LocationListener {
	private final LocationManager mLocationManager;
	private LocationListener mListener = null;

	public LocationListenerProxy(final LocationManager pLocationManager) {
		mLocationManager = pLocationManager;
	}

	public boolean startListening(final LocationListener pListener, final long pUpdateTime,
			final float pUpdateDistance) {
		boolean result = false;
		mListener = pListener;
		for (final String provider : mLocationManager.getProviders(true)) {
			if (LocationManager.GPS_PROVIDER.equals(provider)
					|| LocationManager.NETWORK_PROVIDER.equals(provider)) {
				result = true;
				mLocationManager.requestLocationUpdates(provider, pUpdateTime, pUpdateDistance,
						this);
			}
		}
		return result;
	}

	public void stopListening() {
		mListener = null;
		mLocationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(final Location arg0) {
		if (mListener != null) {
			mListener.onLocationChanged(arg0);
		}
	}

	@Override
	public void onProviderDisabled(final String arg0) {
		if (mListener != null) {
			mListener.onProviderDisabled(arg0);
		}
	}

	@Override
	public void onProviderEnabled(final String arg0) {
		if (mListener != null) {
			mListener.onProviderEnabled(arg0);
		}
	}

	@Override
	public void onStatusChanged(final String arg0, final int arg1, final Bundle arg2) {
		if (mListener != null) {
			mListener.onStatusChanged(arg0, arg1, arg2);
		}
	}
}

 

 

获取当前的位置:

package org.osmdroid.util;

import org.osmdroid.util.constants.UtilConstants;

import android.location.Location;
import android.location.LocationManager;

public class LocationUtils implements UtilConstants {

	/**
	 * This is a utility class with only static members.
	 */
	private LocationUtils() {
	}

	/**
	 * Get the most recent location from the GPS or Network provider.
	 * @param pLocationManager
	 * @return return the most recent location, or null if there's no known location
	 */
	public static Location getLastKnownLocation(final LocationManager pLocationManager) {
		if (pLocationManager == null) {
			return null;
		}
		final Location gpsLocation =
			pLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		final Location networkLocation =
			pLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (gpsLocation == null) {
			return networkLocation;
		} else if (networkLocation == null) {
			return gpsLocation;
		} else {
			// both are non-null - use the most recent
			if (networkLocation.getTime() > gpsLocation.getTime() + GPS_WAIT_TIME) {
				return networkLocation;
			} else {
				return gpsLocation;
			}
		}
	}

}

 

 

具体的使用如下:

启动时候:

   mLocationListener = new LocationListenerProxy(mLocationManager);
   result = mLocationListener.startListening(this, mLocationUpdateMinTime,
     mLocationUpdateMinDistance);

停止时候:

  if (mLocationListener != null) {
   mLocationListener.stopListening();
  }

  mLocationListener = null;

 

针对指南针使用的感应事件代理:

 

package org.osmdroid;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class SensorEventListenerProxy implements SensorEventListener {
	private final SensorManager mSensorManager;
	private SensorEventListener mListener = null;

	public SensorEventListenerProxy(final SensorManager pSensorManager) {
		mSensorManager = pSensorManager;
	}

	public boolean startListening(final SensorEventListener pListener, final int pSensorType,
			final int pRate) {
		final Sensor sensor = mSensorManager.getDefaultSensor(pSensorType);
		if (sensor == null)
			return false;
		mListener = pListener;
		return mSensorManager.registerListener(this, sensor, pRate);
	}

	public void stopListening() {
		mListener = null;
		mSensorManager.unregisterListener(this);
	}

	@Override
	public void onAccuracyChanged(final Sensor pSensor, final int pAccuracy) {
		if (mListener != null) {
			mListener.onAccuracyChanged(pSensor, pAccuracy);
		}
	}

	@Override
	public void onSensorChanged(final SensorEvent pEvent) {
		if (mListener != null) {
			mListener.onSensorChanged(pEvent);
		}
	}

}

 

使用时:

 

  if (mSensorListener == null) {
   mSensorListener = new SensorEventListenerProxy(mSensorManager);
   result = mSensorListener.startListening(this, Sensor.TYPE_ORIENTATION,
     SensorManager.SENSOR_DELAY_UI);
  }

 

	if (mSensorListener != null) {
			mSensorListener.stopListening();
		}

		// Reset values
		mSensorListener = null;

 

 

 

你可能感兴趣的:(osmdroid)