百度地图MyLocation测试代码

package com.example.bydumap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;

public class MainActivity extends MapActivity implements LocationListener {
	private BMapManager mapManager;
	private MapView mapView;
	private MKLocationManager mkLocationManager = null;
	private MyLocationOverlay myLocationOverlay = null;
	private MapController mapController;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mapManager = new BMapManager(getApplication());

		mapManager.init("BC73228901A9978D71A41471D10F53C8EFC9971A", null);
		super.initMapActivity(mapManager);

		mkLocationManager = mapManager.getLocationManager();
		// register location update event
		mkLocationManager.requestLocationUpdates(this);
		// use gps
		mkLocationManager
				.enableProvider((int) MKLocationManager.MK_GPS_PROVIDER);
		mapView = (MapView) findViewById(R.id.map_View);
		// set map mode traffic
		mapView.setTraffic(true);
		mapView.setBuiltInZoomControls(true);
		GeoPoint point = new GeoPoint((int) (26.597239 * 1E6),
				(int) (106.720397 * 1E6));
		mapController = mapView.getController();
		// set map center
		mapController.setCenter(point);
		mapController.setZoom(18);
		myLocationOverlay = new MyLocationOverlay(this, mapView);
		myLocationOverlay.enableMyLocation();
		myLocationOverlay.enableCompass();
		mapView.getOverlays().add(myLocationOverlay);

		new Thread(new MyThread()).start();
	}

	private int x = 31410200;

	Handler mHandler = new Handler() {
		public void handleMessage(Message message) {
			// Toast.makeText(MainActivity.this, "send!", 1).show();
			x += 800;
			GeoPoint point = new GeoPoint(x, 120900077);
			mapController.animateTo(point);

			addOverLay(point);
			
			super.handleMessage(message);
		};
	};
	
	/*** 
     * 添加overlay 
     */  
    void addOverLay(GeoPoint geoPoint) {  
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),  
                R.drawable.point);  
        MyOverlay overlay = new MyOverlay(mapView, bitmap);  
        overlay.setGeoPoint(geoPoint);  
//        mapView.getOverlays().clear();
        if(lastOverlay != null)
        	mapView.getOverlays().remove(lastOverlay);
        mapView.getOverlays().add(overlay);//添加自定义overlay  
        lastOverlay = overlay;
    }
    
    private  MyOverlay lastOverlay = null;

	public class MyOverlay extends Overlay {  
	    private MapView mapView;  
	    private Projection projection;  
	    private Point point;// 屏幕对象的点  
	    private Bitmap bitmap;  
	  
	    private GeoPoint geoPoint;// 经纬度点  
	  
	    public void setGeoPoint(GeoPoint geoPoint) {  
	        this.geoPoint = geoPoint;  
	    }  
	  
	    public MyOverlay(MapView mapView, Bitmap bitmap) {  
	        super();  
	        this.mapView = mapView;  
	        this.bitmap = bitmap;  
	    }  
	  
	    @Override  
	    public void draw(Canvas canvas, MapView arg1, boolean arg2) {  
	        projection = mapView.getProjection();  
	        point = new Point();  
	        projection.toPixels(geoPoint, point);// 将GeoPoint 转换成point.  
	        int x = point.x - bitmap.getWidth() / 2;  
	        int y = point.y - bitmap.getHeight();  
	  
	        canvas.drawBitmap(bitmap, x, y, new Paint());  
	    }  
	} 
	
	class MyThread implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < 100; i++) {
				try {
					Thread.sleep(3000);
					Message message = new Message();
					mHandler.sendMessage(message);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onDestroy() {
		if (mapManager != null) {
			mapManager.destroy();
			mapManager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mapManager != null) {
			mapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mapManager != null) {
			mapManager.start();
		}
		super.onResume();
	}

	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		Toast.makeText(MainActivity.this, "Location changed!",
				Toast.LENGTH_SHORT).show();
		System.out.println("" + (int) (location.getLatitude() * 1000000) + ", "
				+ (int) (location.getLongitude() * 1000000));
		if (location != null) {
			final GeoPoint pt = new GeoPoint(
					(int) (location.getLatitude() * 1000000),
					(int) (location.getLongitude() * 1000000));
			// mapController.setCenter(pt);
		}
	}

	@Override
	protected boolean isLocationDisplayed() {
		// TODO Auto-generated method stub
		return myLocationOverlay.isMyLocationEnabled();
	}

}


你可能感兴趣的:(百度地图MyLocation测试代码)