package com.baidu.mapapi.demo;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;
import com.baidu.mapapi.*;
public class ItemizedOverlayDemo extends MapActivity {
static View mPopView = null; // ���markʱ����������View
static MapView mMapView = null;
int iZoom = 0;
OverItemT overitem = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewdemo);
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
}
app.mBMapMan.start();
// ���ʹ�õ�ͼSDK�����ʼ����ͼActivity
super.initMapActivity(app.mBMapMan);
mMapView = (MapView)findViewById(R.id.bmapView);
mMapView.setBuiltInZoomControls(true);
//���������Ŷ���������Ҳ��ʾoverlay,Ĭ��Ϊ������
mMapView.setDrawOverlayWhenZooming(true);
GeoPoint point =new GeoPoint((int)(39.90923*1e6), (int)(116.397428*1e6));
mMapView.getController().setCenter(point);
// ���ItemizedOverlay
Drawable marker = getResources().getDrawable(R.drawable.iconmarka); //�õ���Ҫ���ڵ�ͼ�ϵ���Դ
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight()); //Ϊmaker����λ�úͱ߽�
overitem = new OverItemT(marker, this, 3);
mMapView.getOverlays().add(overitem); //���ItemizedOverlayʵ����mMapView
// �������markʱ�ĵ�������
mPopView=super.getLayoutInflater().inflate(R.layout.popview, null);
mMapView.addView( mPopView,
new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
null, MapView.LayoutParams.RIGHT));
mPopView.setVisibility(View.GONE);
iZoom = mMapView.getZoomLevel();
// mMapView.regMapViewListener(app.mBMapMan, new MKMapViewListener(){
// public void onMapMoveFinish() {
// if(iZoom != mMapView.getZoomLevel())
// {// �����߲�һ��
// iZoom = mMapView.getZoomLevel();
// if(overitem.mGeoList.size() > 0)
// overitem.mGeoList.remove(0);
// //overitem.updateOverlay();
//// Drawable marker = getResources().getDrawable(R.drawable.iconmarka); //�õ���Ҫ���ڵ�ͼ�ϵ���Դ
//// marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
//// .getIntrinsicHeight()); //Ϊmaker����λ�úͱ߽�
//// mMapView.getOverlays().add(new OverItemT(marker, ItemizedOverlayDemo.this, 3));
// }
// }
// });
}
@Override
protected void onPause() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
if(app.mBMapMan != null)
app.mBMapMan.stop();
super.onPause();
}
@Override
protected void onResume() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
app.mBMapMan.start();
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
class OverItemT extends ItemizedOverlay<OverlayItem> {
public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
private Drawable marker;
private Context mContext;
private double mLat1 = 39.90923; // point1�
private double mLon1 = 116.357428; // point1����
private double mLat2 = 39.90923;
private double mLon2 = 116.397428;
private double mLat3 = 39.90923;
private double mLon3 = 116.437428;
public OverItemT(Drawable marker, Context context, int count) {
super(boundCenterBottom(marker));
this.marker = marker;
this.mContext = context;
// �ø����ľ�γ�ȹ���GeoPoint����λ���� (�� * 1E6)
GeoPoint p1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
GeoPoint p2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6));
// ����OverlayItem��������������Ϊ��item��λ�ã������ı�������Ƭ��
mGeoList.add(new OverlayItem(p1, "P1", "point1"));
mGeoList.add(new OverlayItem(p2, "P2", "point2"));
if(count == 3)
{
GeoPoint p3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6));
mGeoList.add(new OverlayItem(p3, "P3", "point3"));
}
populate(); //createItem(int)��������item��һ���������ݣ��ڵ�����������ǰ�����ȵ����������
}
public void updateOverlay()
{
populate();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Projection�ӿ�������Ļ��������;�γ������֮��ı任
Projection projection = mapView.getProjection();
for (int index = size() - 1; index >= 0; index--) { // ����mGeoList
OverlayItem overLayItem = getItem(index); // �õ�����������item
String title = overLayItem.getTitle();
// �Ѿ�γ�ȱ任�������MapView���Ͻǵ���Ļ��������
Point point = projection.toPixels(overLayItem.getPoint(), null);
// ���ڴ˴�������Ļ��ƴ���
Paint paintText = new Paint();
paintText.setColor(Color.BLUE);
paintText.setTextSize(15);
canvas.drawText(title, point.x-30, point.y, paintText); // �����ı�
}
super.draw(canvas, mapView, shadow);
//����һ��drawable�߽磬ʹ�ã�0��0�������drawable�ײ����һ�����ĵ�һ������
boundCenterBottom(marker);
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mGeoList.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mGeoList.size();
}
@Override
// ��������¼�
protected boolean onTap(int i) {
setFocus(mGeoList.get(i));
// ��������λ��,��ʹ֮��ʾ
GeoPoint pt = mGeoList.get(i).getPoint();
ItemizedOverlayDemo.mMapView.updateViewLayout( ItemizedOverlayDemo.mPopView,
new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
pt, MapView.LayoutParams.BOTTOM_CENTER));
ItemizedOverlayDemo.mPopView.setVisibility(View.VISIBLE);
Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return true;
}
// @Override
// public boolean onTap(GeoPoint arg0, MapView arg1) {
// // TODO Auto-generated method stub
// // ��ȥ����������
// ItemizedOverlayDemo.mPopView.setVisibility(View.GONE);
// return super.onTap(arg0, arg1);
// }
}