GIS的学习(十三)Osmdroid中地图中添加自定义指南针

实现指南针代码:

package com.geoserver.osmdroid;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
/**
 * 自定义指南针实现
 * 
 * @Title: 
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:
 * @Date:2012-8-24
 * @author  longgangbai
 * @version 1.0
 */
public class CompassView extends View {
	private Drawable mCompass;
	private float mAzimuth = 0;
	private boolean mSideBottom;
	private int PADDING = 2;

	public CompassView(Context ctx, final boolean sideBottom) {
		super(ctx);

		this.mCompass = ctx.getResources().getDrawable(R.drawable.arrow_n);
		mSideBottom = sideBottom;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		if (mSideBottom) {
			canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth() / 2, PADDING + mCompass.getMinimumHeight() / 2);
			mCompass.setBounds(PADDING, PADDING, PADDING + mCompass.getMinimumWidth(), PADDING
					+ mCompass.getMinimumHeight());
		} else {
			canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth() / 2, this.getHeight() - mCompass.getMinimumHeight() /2 - PADDING);
			mCompass.setBounds(PADDING, this.getHeight() - mCompass.getMinimumHeight() - PADDING, PADDING
					+ mCompass.getMinimumWidth(), this.getHeight() - PADDING);
		}
		mCompass.draw(canvas);
		canvas.restore();

		super.onDraw(canvas);
	}

	public void setAzimuth(float aAzimuth) {
		mAzimuth = aAzimuth;
	}

}

 

 

界面类:

package com.geoserver.osmdroid;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
/**
 * 
	官网:
	http://code.google.com/p/osmdroid/
	功能:
	基本功能有:查看地图、缩放地图、单点移动、多点触控等
 * @Title: 
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:易程科技股份有限公司
 * @Date:2012-7-31
 * @author  longgangbai
 * @version 1.0
 */
public class GeoServerosmdroidActivity extends Activity  {

	private MapView mapView;

	private CompassView compassView;
	private SensorManager sensorManager;
	private boolean mCompassEnabled=true;
	private boolean mDrivingDirectionUp=true;
	private boolean mNorthDirectionUp=true;
	private boolean sideBottom=true;
	private float mLastSpeed;
	private RelativeLayout relationLayout;
	private MapController mapController;

	private final SensorEventListener mListener = new SensorEventListener() {
		private int iOrientation = -1;

		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}

		public void onSensorChanged(SensorEvent event) {
			if (iOrientation < 0)
				iOrientation = ((WindowManager) GeoServerosmdroidActivity.this.getSystemService(Context.WINDOW_SERVICE))
						.getDefaultDisplay().getOrientation();

			compassView.setAzimuth(event.values[0] + 90 * iOrientation);
			compassView.invalidate();

			if (mCompassEnabled)
				if (mNorthDirectionUp)
					if (mDrivingDirectionUp == false || mLastSpeed == 0) {
						mapView.invalidate();
					}
		}

	};



	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // Pass true here to actually contribute to OSM!
        setContentView(R.layout.main);
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
	    sensorManager.registerListener(mListener, sensorManager
				.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
        initCompassView();
		initgeoServerMap();
    }

    /**
     * 
     */
	private void initgeoServerMap() {
		mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setBuiltInZoomControls(true);
		mapController = mapView.getController();
		mapController.setZoom(9);
		GeoPoint point2 = new GeoPoint(-99.404296875*1000000, 42.682435398386204*1000000);         
		mapController.setCenter(point2); 
		this.setRequestedOrientation(-1);
        compassView.setVisibility(View.VISIBLE);
	}

	/**
	 * 鍒濆鍖栨寚鍗楅拡
	 */
	private void initCompassView() {
		relationLayout=(RelativeLayout)findViewById(R.id.geoserver_layout);
        this.mapView =(MapView)findViewById(R.id.map);
        	
        /* Compass */
    	compassView = new CompassView(this, sideBottom);
        compassView.setVisibility(mCompassEnabled ? View.VISIBLE : View.INVISIBLE);
        /* Create RelativeLayoutParams, that position in in the top right corner. */
        final RelativeLayout.LayoutParams compassParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        compassParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        compassParams.addRule(!sideBottom ? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.ALIGN_PARENT_TOP);
        relationLayout.addView(compassView, compassParams);
	}

	@Override
	protected void onDestroy() {
		sensorManager.unregisterListener(mListener);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		sensorManager.unregisterListener(mListener);
	}

	@Override
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(mListener, sensorManager
				.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
	}
}

 

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#FFFFFF"
    android:id="@+id/geoserver_layout"
    android:layout_height="fill_parent" >
    <org.osmdroid.views.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:enabled="true"
        tilesource="MapquestOSM"  
        />

</RelativeLayout>

 

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.geoserver.osmdroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
    <application     android:icon="@drawable/ic_launcher"     
        android:label="@string/app_name" >     
        <activity         android:name="GeoServerosmdroidActivity"         
            android:label="@string/app_name" >         
            <intent-filter>             
                <action android:name="android.intent.action.MAIN" />              
                <category android:name="android.intent.category.LAUNCHER" />         
			</intent-filter>     
		</activity> 
	</application> 
</manifest>

 

 

 

你可能感兴趣的:(compassView,osmdroid,指南针,openstreetMap)