google Map的一个简单实例

public class mapActivity extends MapActivity {
	private MapView mMapView;
	private MapController mMapController; 
	private GeoPoint mGeoPoint;
    
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplayout_1);
        
        mMapView = (MapView)findViewById(R.id.mapView_1);
        mMapController = mMapView.getController(); 
        
        mMapView.setTraffic(false);			//traffic mode
		//mMapView.setSatellite(true); 		//satellite mode
		//mMapView.setStreetView(false);	//street mode

        //mMapView.displayZoomControls(true);      //老版中用这个,新版中用下面的
        mMapView.setBuiltInZoomControls(true); 

        //String coordinates[] = {"23.15345", "113.343329"};
        //String coordinates[]={"1.352566007", "103.78921587"};
        String coordinates[] = {"23.141126", "113.34528"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
 
		mGeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
 
        mMapController.animateTo(mGeoPoint);
        mMapController.setZoom(17);
        
        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mMapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        
        mMapView.invalidate();
    }
    
    class MapOverlay extends Overlay
    {
        @Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when)
        {
			super.draw(canvas, mapView, shadow);

			// ---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			// 将经纬度转换成实际屏幕坐标
			mapView.getProjection().toPixels(mGeoPoint, screenPts);

			// ---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.set);
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
			return true;
        }

		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			// 显示经纬度
			if (event.getAction() == 1) {
				//System.out.println("touch");
				GeoPoint p = mMapView.getProjection().fromPixels(
						(int) event.getX(), (int) event.getY());
				Toast.makeText(getBaseContext(),
						p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() / 1E6,
						Toast.LENGTH_SHORT).show();
			}
			return super.onTouchEvent(event, mapView);
		} 
    } 

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			mMapController.zoomIn();
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			mMapController.zoomOut();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
    
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
}

你可能感兴趣的:(Google,UP)