(Android 地图开发) 高德地图添加覆盖物

来源:互联网 发布:青年网络公开课观后感 编辑:程序博客网 时间:2024/04/29 19:19

效果截图:

源代码:

package com.rf.mapabc;import com.amap.mapapi.core.GeoPoint;import com.amap.mapapi.map.MapActivity;import com.amap.mapapi.map.MapController;import com.amap.mapapi.map.MapView;import com.amap.mapapi.map.Overlay;import com.amap.mapapi.map.Projection;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Point;import android.os.Bundle;import android.os.Handler;import android.os.Message;public class MainActivity extends MapActivity {private MapView mMapView;private MapController mMapController;private GeoPoint point;// 百度地图LBS定位private LocationClient mLocationClient;private MyLocationListener locationlistener;// 主线程Handlerprivate Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case 0:Bundle bundle = msg.getData();double lat = bundle.getDouble("x");double lng = bundle.getDouble("y");point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));MyOverlays overlay = new MyOverlays(point);mMapController.setCenter(point); // 设置地图中心点mMapController.setZoom(12); // 设置地图zoom级别mMapView.getOverlays().clear();mMapView.getOverlays().add(overlay);break;}}};@Override/** *显示栅格地图,启用内置缩放控件,并用MapController控制地图的中心点及Zoom级别 */public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.mapview);// 获取地图组件mMapView = (MapView) findViewById(R.id.mapView);mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放point = new GeoPoint((int) (39.982378 * 1E6), (int) (116.304923 * 1E6)); // 用给定的经纬度构造一个GeoPoint,单位是微度// (度// *// 1E6)mMapController.setCenter(point); // 设置地图中心点mMapController.setZoom(12); // 设置地图zoom级别// 百度地位mLocationClient = new LocationClient(this);locationlistener = new MyLocationListener(handler);mLocationClient.registerLocationListener(locationlistener);mLocationClient.start();if (mLocationClient != null && mLocationClient.isStarted()) {setLocationOption();mLocationClient.requestLocation();}}// 实时位置的显示的问题(LBS)public class MyLocationListener implements BDLocationListener {private Handler handle;public MyLocationListener(Handler handle) {super();this.handle = handle;}@Overridepublic void onReceiveLocation(BDLocation location) {// TODO Auto-generated method stub// 获取经纬度double x = location.getLatitude();double y = location.getLongitude();Message message = new Message();Bundle bundle = new Bundle();bundle.putDouble("x", x);bundle.putDouble("y", y);message.setData(bundle);message.what = 0;handle.sendMessage(message);}@Overridepublic void onReceivePoi(BDLocation arg0) {// TODO Auto-generated method stub}}// 查询条件显示private void setLocationOption() {LocationClientOption option = new LocationClientOption();option.setOpenGps(true);option.setAddrType("all");// 返回的定位结果包含地址信息option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000msoption.disableCache(true);// 禁止启用缓存定位option.setPoiNumber(5); // 最多返回POI个数option.setPoiDistance(1000); // poi查询距离option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息mLocationClient.setLocOption(option);}// 地图添加覆盖物(Overlay覆盖物的基类)public class MyOverlays extends Overlay {// 经纬度坐标GeoPoint gp;public MyOverlays(GeoPoint gp) {super();this.gp = gp;}@Overridepublic void draw(Canvas canvas, MapView mapView, boolean arg2) {// TODO Auto-generated method stub// 获取mapView 的Projection对象Projection projection = mapView.getProjection();Point point = new Point();// 将地理位置转化为屏幕坐标projection.toPixels(gp, point);// 画笔相关设置Paint paintText = new Paint();paintText.setColor(Color.BLUE);paintText.setTextSize(18);// 图片资源Bitmap pic = BitmapFactory.decodeResource(getResources(),R.drawable.da_marker_red); // 得到Bitmap对象canvas.drawBitmap(pic, point.x - 20, point.y - 32, paintText); // 绘图// 绘制文本paintText.setAlpha(18);canvas.drawCircle(point.x, point.y, 120, paintText);}}}


 

原创粉丝点击