要替换Google Map静态图片中缺省的Marker为您自己的图标,需要使用
javax.microedition.lcdui.Graphics.drawImage(Image img, int x, int y, int anchor)
其中anchor为图像在屏幕上的停靠点。
int | 常量 | 相当(x,y) | |
0或20 | Graphics.SOLID 或Graphics.LEFT|Graphics.TOP | 图片左上角为(x,y) |
|
3 | Graphics.DOTTED|Graphics.VCENTER 或Graphics.HCENTER|Graphics.VCENTER | 图片中心为(x,y) | |
6 | Graphics.LEFT|Graphics.VCENTER | 图片左边中点为(x,y) | |
10 | Graphics.RIGHT|Graphics.VCENTER | 图片右边中点为(x,y) | |
17 | Graphics.DOTTED|Graphics.TOP 或Graphics.HCENTER|Graphics.TOP | 图片上边中点为(x,y) | |
24 | Graphics.RIGHT|Graphics.TOP | 图片右上角为(x,y) | |
33 | Graphics.BOTTOM|Graphics. DOTTED 或Graphics.BOTTOM|Graphics.HCENTER | 图片下边中点为(x,y) | |
36 | Graphics.BOTTOM|Graphics.LEFT | 图片左下角为(x,y) | |
40 | Graphics.BOTTOM|Graphics.RIGHT | 图片右下角为(x,y) |
然后需要把Marker的经纬度转换成屏幕坐标,可以使用如下的函数:
public static boolean map2screen(double mx,double my,YPoint pt,double cx,double cy,YRect rt,int zoom)
{
if(Math.abs(cx-0.00001)<0.0001 && Math.abs(cy-0.00001)<0.0001)
return false;
double size = MyMath.pow(2,zoom);
double spanY = 360.0/size*rt.width()/256;
double spanX = 360.0/size*rt.height()/256;
pt.y = (int) (((cx - mx) / spanX + 0.5) * rt.height());
pt.x = (int) (((my - cy) / spanY + 0.5) * rt.width());
return true;
}
最后在你的Canvas paint函数中绘制这个图标:
MyRect rt = new MyRect(0,0,this.getHeight(),this.getWidth());
MyUtils.map2screen(slatitude,slongitude,mypos,latitude,longitude,rt,zoom);
g.drawImage(imgPos, mypos.x, mypos.y, Graphics.BOTTOM|Graphics.DOTTED);