osmdroid API解读(十四)

osmdroid API解读(十四)

osmdroid-android org.osmdroid.views.overlay.compass包

1. IOrientationConsumer

方向改变监听器所获取的方向为磁北方向,并非真北方向。

public interface IOrientationConsumer
{

    void onOrientationChanged(float orientation, IOrientationProvider source);
}

2. IOrientationProvider

一个方向提供者接口,用于启动方向检测,并回传给IOrientationConsumer接口。

public interface IOrientationProvider
{
    boolean startOrientationProvider(IOrientationConsumer orientationConsumer);

    void stopOrientationProvider();

    float getLastKnownOrientation();

    void destroy();
}

3. InternalCompassOrientationProvider

实现了IOrientationProvider接口,将Google API提供的SensorManager进行封装,提供了新的、简单的接口(IOrientationProvider)。

public class InternalCompassOrientationProvider implements SensorEventListener, IOrientationProvider
{
    private IOrientationConsumer mOrientationConsumer;
    private SensorManager mSensorManager;
    private float mAzimuth;

    public InternalCompassOrientationProvider(Context context)
    {
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    }

    //开启方向监听
    @Override
    public boolean startOrientationProvider(IOrientationConsumer orientationConsumer)
    {
        mOrientationConsumer = orientationConsumer;
        boolean result = false;

        final Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        if (sensor != null) {
            result = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
        }
        return result;
    }

    @Override
    public void stopOrientationProvider()
    {
        mOrientationConsumer = null;
        mSensorManager.unregisterListener(this);
    }

    @Override
    public float getLastKnownOrientation()
    {
        return mAzimuth;
    }

    @Override
    public void destroy() {
        stopOrientationProvider();
        mOrientationConsumer=null;
        mSensorManager=null;
    }

    @Override
    public void onAccuracyChanged(final Sensor sensor, final int accuracy)
    {
        // This is not interesting for us at the moment
    }

    @Override
    public void onSensorChanged(final SensorEvent event)
    {
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            if (event.values != null) {
                mAzimuth = event.values[0];
                if (mOrientationConsumer != null)
                    mOrientationConsumer.onOrientationChanged(mAzimuth, this);
            }
        }
    }
}

4. CompassOverlay

利用上面封装的罗盘,提供了一个罗盘Overlay。

public class CompassOverlay extends Overlay implements IOverlayMenuProvider, IOrientationConsumer {

    ...

}

osmdroid-android org.osmdroid.views.overlay.mylocation包

1. IMyLocationConsumer

位置监听器

public interface IMyLocationConsumer {

    void onLocationChanged(Location location, IMyLocationProvider source);
}

2. IMyLocationProvider

位置提供者应该有的接口。

public interface IMyLocationProvider {
boolean startLocationProvider(IMyLocationConsumer myLocationConsumer);

void stopLocationProvider();

Location getLastKnownLocation();

void destroy();

}

3. GpsMyLocationProvider

利用Google API实现的gps提供者,使用LocationManager#GPS_PROVIDER、LocationManager#NETWORK_PROVIDER两种模式。

public class GpsMyLocationProvider implements IMyLocationProvider, LocationListener {
    private LocationManager mLocationManager;
    private Location mLocation;

    private IMyLocationConsumer mMyLocationConsumer;
    private long mLocationUpdateMinTime = 0;
    private float mLocationUpdateMinDistance = 0.0f;
    private NetworkLocationIgnorer mIgnorer = new NetworkLocationIgnorer();
    private final Set<String> locationSources = new HashSet<>();

    public GpsMyLocationProvider(Context context) {
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationSources.add(LocationManager.GPS_PROVIDER);
    }

    ...

}

4. 位置Overlay

osmdroid里面提供了三个不同的提供位置的Overlay:

  • SimpleLocationOverlay
  • DirectedLocationOverlay
  • MyLocationNewOverlay
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值