創建MapOverlay GeoPoints陣列並覆蓋繪製函數:
public class MapOverlay extends Overlay
{
private ArrayList<GeoPoints>points;
...
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
int len = points.size();
if(len > 0)
{
for(int i = 0; i < len; i++)
{
// do with your points whatever you want
// you connect them, draw a bitmap over them and etc.
// for example:
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pointer);
mapView.getProjection().toPixels(points.get(i), screenPts);
canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y - bmp.getHeight()/2, null);
}
}
}
public void addPoint(GeoPoint p)
{
// add point to the display array
}
public void removePointByIndex(int i)
{
points.remove(i);
}
public void removePointByCordinate(Double lat, Double lng)
{
int index = -1;
int len = points.size();
if(len > 0)
{
for(int i = 0; i < len; i++)
{
if((int)(lat*1E6) == points.get(i).getLatitudeE6() && (int)(lng*1E6) == points.get(i).getLongitudeE6())
{
index = i;
}
}
}
if(index != -1)
{
points.remove(index);
}
}
}
public void removePoint(GeoPoint p)
{
int index = -1;
int len = points.size();
if(len > 0)
{
for(int i = 0; i < len; i++)
{
if(p == points.get(i))
{
index = i;
}
}
}
if(index != -1)
{
points.remove(index);
}
}
}
}
(我沒有測試上面的類)
,然後在MapActivity類你可以:
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setClickable(true);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mapOverlay);
嘗試谷歌的一些谷歌地圖教程,也許你會發現更多的解決方案。
謝謝你爲回答我而付出的巨大努力。你在那裏給出的好的代碼。我可以理解很多,但我沒有看到實際上可以刪除某個特定點的部分。方法removePoint(GeoPoint p),應該在那裏做什麼?這是令我困擾的事情。 另外,在你提到MapActivity類的地方,這些命令不會清除listOfOverlays中的所有點嗎? – bubbly
這個方法你在開始時只調用一次,我從我的代碼複製它。你以前不需要清除。 – goodm
removePoint(GeoPoint p)只是從數組中刪除一個點嗎?但是,如果我發送座標,它怎麼知道我說的是哪一點? (geopoint) – bubbly