这是效果图,屏幕中的位置是兴东地铁站,左下方是洪浪北地铁站
主要的算法公式为:
以A为中心点,已知其经纬度为 lonA, latA
已知其他位置坐标的经度为纬度 lon, lat
其座标在手机屏幕位置:
double x = ((lon-lonA)*111*Math.cos(lat/(2*π)));
double y = (lat-latA)*111;
代码如下:
一、将经纬度转换成手机屏幕的座标,并设置到ShipInfo对象中
public staticList makeScreenPoint() {
//兴东
doubleLonA =113.91902814890139;
doublelatA =22.58129196196562;
List list =newArrayList<>();
//洪浪北
doubleLonB =113.91055236841433;
doubleLatB =22.57404034726216;
doubleπ =3.1415926535898;
doubleYb= ((LatB-latA)*111);
doubleXb= ((LonB-LonA)*111*Math.cos(LatB/(2*π)));
ShipInfo info1 =newShipInfo(0, -Xb,-Yb);
list.add(info1);
//留仙洞
doubleLonC =113.94364009882204;
doubleLatC =22.580182450018718;
doubleYc= ((LatC-latA)*111);
doubleXc= ((LonC-LonA)*111*Math.cos(LatC/(2*π)));
ShipInfo info2 =newShipInfo(1, -Xc,-Yc);
list.add(info2);
//TCL
doubleLond =113.92696748758547;
doubleLatd =22.567303532706593;
doubleYd= ((Latd-latA)*111);
doubleXd= ((Lond-LonA)*111*Math.cos(Latd/(2*π)));
ShipInfo info3 =newShipInfo(2, -Xd,-Yd);
list.add(info3);
//文光村
doubleLone =113.949396119368;
doubleLate =22.569195818282925;
doubleYe= ((Late-latA)*111);
doubleXe= ((Lone-LonA)*111*Math.cos(Late/(2*π)));
ShipInfo info4 =newShipInfo(3, -Xe,-Ye);
list.add(info4);
returnlist;
}
//shipinfo类
public classShipInfo {
private intship;//什么船
private doublelon;//经度
private doublelat;//纬度
publicShipInfo(intship,doublelon,doublelat){
this.ship= ship;
this.lon= lon;
this.lat= lat;
}
public doublegetLon() {
returnlon;
}
public voidsetLon(doublelon) {
this.lon= lon;
}
public intgetShip() {
returnship;
}
public voidsetShip(intship) {
this.ship= ship;
}
public doublegetLat() {
returnlat;
}
public voidsetLat(doublelat) {
this.lat= lat;
}
}
二、在自定控件中调用canvas.drawBitmap(bimap,x,y,mPaint);
需要注意的在转换到屏幕座标时 中心点的位置最开始是在手机屏幕原点座标的,所以需要将所有座标平移到屏幕座标
for(ShipInfo shipInfo:list){
floatx = (float) shipInfo.getLon()*90+getWidth()/2;
floaty = (float) shipInfo.getLat()*90+getHeight()/2;
}
其中90代表的是比例,可任意设值,值越大,两个位置之间的距离在屏幕上显示的越大