[百度地圖]修改我的位置的圖示(附原始碼)
阿新 • • 發佈:2019-01-31
之前在網上看到有人寫過這個教程,但是給的程式碼都一塊一塊的,沒有給原始碼,看得非常辛苦,這讓我們這些初學者情何以堪
自己研究好久終於研究出來了,在這裡分享給大家!(轉載請註明謝謝!)
開始沒有修改時:
主要是要自定一個內部類繼承自MyLocationOverlay
// 自定義myloactionoverlay public class MyOverLay extends MyLocationOverlay {
public Bitmap bitmap;
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public MyOverLay(Context arg0, MapView arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
// 重寫描繪我的位置的方法
@Override
protected void drawMyLocation(Canvas canvas, MapView mapView,
Location location, GeoPoint geoPoint, long when) {
try {
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(geoPoint, point);
// point獲取到的是我們在螢幕上的點.而我們要顯示的圖示應該在這個點的正上方
int x = point.x - bitmap.getWidth();
int y = point.y - bitmap.getHeight();
canvas.drawBitmap(bitmap, x, y, new Paint());
} catch (Exception e) {
// TODO: handle exception
super.drawMyLocation(canvas, mapView, location, geoPoint, when);
}
}
}
然後新增一個方法進行例項化,傳入bitmap
//新增自己定義的overlay
public void addMyOverLay() {
myOverLay = new MyOverLay(this, mapView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.p);//自定義圖示
myOverLay.setBitmap(bitmap);
mapView.getOverlays().add(myOverLay);//把我的位置加入地圖
}
其他都是原來的百度定位的一些方法不用變
效果:
這樣那個藍色標誌就變成了QQ了
如果對你有幫助請頂貼..........
原始碼回覆可見:
自己研究好久終於研究出來了,在這裡分享給大家!(轉載請註明謝謝!)
開始沒有修改時:
主要是要自定一個內部類繼承自MyLocationOverlay
// 自定義myloactionoverlay public class MyOverLay extends MyLocationOverlay {
public Bitmap bitmap;
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public MyOverLay(Context arg0, MapView arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
// 重寫描繪我的位置的方法
@Override
protected void drawMyLocation(Canvas canvas, MapView mapView,
Location location, GeoPoint geoPoint, long when) {
try {
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(geoPoint, point);
// point獲取到的是我們在螢幕上的點.而我們要顯示的圖示應該在這個點的正上方
int x = point.x - bitmap.getWidth();
int y = point.y - bitmap.getHeight();
canvas.drawBitmap(bitmap, x, y, new Paint());
} catch (Exception e) {
// TODO: handle exception
super.drawMyLocation(canvas, mapView, location, geoPoint, when);
}
}
}
然後新增一個方法進行例項化,傳入bitmap
//新增自己定義的overlay
public void addMyOverLay() {
myOverLay = new MyOverLay(this, mapView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.p);//自定義圖示
myOverLay.setBitmap(bitmap);
mapView.getOverlays().add(myOverLay);//把我的位置加入地圖
}
其他都是原來的百度定位的一些方法不用變
效果:
這樣那個藍色標誌就變成了QQ了
如果對你有幫助請頂貼..........
原始碼回覆可見: