package xiaosi.baiduMap;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
public class BaiduMapActivity extends MapActivity
{
/** Called when the activity is first created. */
private BMapManager mapManager = null;
private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
private MapView mapView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapManager = new BMapManager(getApplication());
mapManager.init(key, null);
super.initMapActivity(mapManager);
mapView = (MapView) findViewById(R.id.mapsView);
mapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
MapController mapController = mapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
mapController.setZoom(12); // 设置地图zoom级别
Drawable marker = getResources().getDrawable(R.drawable.d); //得到需要标在地图上的资源
mapView.getOverlays().add(new OverItemT(marker, this)); //添加ItemizedOverlay实例到mMapView
}
class OverItemT extends ItemizedOverlay<OverlayItem>
{
private List<OverlayItem> GeoPointList = new ArrayList<OverlayItem>();
private Context mContext;
//三个坐标
private double coordinateX1= 39.90923;
private double coordinateY1 = 116.397428;
private double coordinateX2 = 39.9022;
private double coordinateY2 = 116.3922;
private double coordinateX3 = 39.917723;
private double coordinateY3 = 116.3722;
private double coordinateX4 = 39.90923;
private double coordinateY4 = 116.3922;
public OverItemT(Drawable marker, Context context)
{
//调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素。
super(boundCenterBottom(marker));
this.mContext = context;
// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
GeoPoint point1 = new GeoPoint((int) (coordinateX1 * 1E6), (int) (coordinateY1 * 1E6));
GeoPoint point2 = new GeoPoint((int) (coordinateX2 * 1E6), (int) (coordinateY2 * 1E6));
GeoPoint point3 = new GeoPoint((int) (coordinateX3 * 1E6), (int) (coordinateY3 * 1E6));
GeoPoint point4 = new GeoPoint((int) (coordinateX4 * 1E6), (int) (coordinateY4 * 1E6));
/*OverlayItem(GeoPoint point, String title, String snippet)
* point 该item的位置
* title 该item的标题文本
* snippet 该item的文字片段
*/
GeoPointList.add(new OverlayItem(point1, "P1", "写字楼"));
GeoPointList.add(new OverlayItem(point2, "P2", "西城区政府"));
GeoPointList.add(new OverlayItem(point3, "P3", "招待所"));
GeoPointList.add(new OverlayItem(point4, "P4", "小学"));
// createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
populate();
}
@Override
protected OverlayItem createItem(int i)
{
return GeoPointList.get(i);
}
@Override
public int size()
{System.out.println("Size;" + GeoPointList.size());
return GeoPointList.size();
}
@Override
// 处理当点击事件
protected boolean onTap(int i)
{
//getSnippet() 该item的文字片段
Toast.makeText(this.mContext, GeoPointList.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return true;
}
}
@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();
}
}
评论4
最新资源