[java]
package com.baidu.map;
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.location.Location;
import android.location.LocationManager;
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.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.PoiOverlay;
import com.baidu.mapapi.Projection;
public class BmapActivity extends MapActivity {
// 定义地图引擎管理类
private BMapManager mapManager;// 定义搜索服务类
private MapView mapView;
private MapController mapController;
LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove
MyLocationOverlay mLocationOverlay = null; // 定位图层
private double dLat;
private double dLon;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化MapActivity
mapManager = new BMapManager(getApplication());
// init方法的第一个参数需填入申请的APIKey
mapManager.init("DE7BC8B377AFA895ED0C16F504B5C4FC5E3E149B", null);
super.initMapActivity(mapManager);
mapView = (MapView) findViewById(R.id.map_View);
// 设置地图模式为交通地图
// mapView.setTraffic(true);
// 设置启用内置的缩放控件
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
// 设置在缩放动画过程中也显示overlay,默认为不绘制
mapView.setDrawOverlayWhenZooming(true);
mapController = mapView.getController();
mapController.setZoom(16);
//定位自己的位置
myself();
// 添加定位图层
mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);
Drawable marker = getResources().getDrawable(R.drawable.da_marker_red);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());// Intrinsic固有
mapView.getOverlays().add(new MyItemizedOverlay(marker, this));
}
// 同一类型覆盖物的绘制
class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
// 属性
private Drawable marker;
private Context mContext;
private List<OverlayItem> geoList = new ArrayList<OverlayItem>();
// 构造方法
public MyItemizedOverlay(Drawable marker, Context context) {
super(boundCenterBottom(marker));
this.marker = marker;
this.mContext = context;
// 构造地理坐标
GeoPoint p1 = new GeoPoint((int) (dLat * 1E6), (int) (dLon * 1E6));
geoList.add(new OverlayItem(p1, "P1", "这是我的当前位置"));
populate();// 执行填充方法
}
// 绘制方法
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// 投影,用于屏幕像素点坐标系统与地球经纬度点坐标系统的转换
Projection projection = mapView.getProjection();
for (int index = size() - 1; index >= 0; index--) {
OverlayItem overlayItem = this.getItem(index);
String title = overlayItem.getTitle();
Point point = projection.toPixels(overlayItem.getPoint(), null);
Paint painttext = new Paint();
painttext.setColor(Color.BLACK);
painttext.setTextSize(15); &n
补充:移动开发 , Android ,