(Android 地图开发) 高德地图添加覆盖物
无情
阅读:770
2021-04-01 10:20:55
评论:0
效果截图:
源代码:
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;
// 主线程Handler
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (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;
}
@Override
public 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);
}
@Override
public 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");// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
option.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;
}
@Override
public 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);
}
}
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。