我试图在来自 SQLite db 的谷歌地图上添加标记我有一个列表视图,我可以在其中看到来自 db 的纬度和经度。所以我知道数据库正在工作。我是 java 和 android 的新手。但我希望越来越好。我不确定我的代码是否正确,需要一些帮助。这是我写的代码:
列出 mapOverlays = mapView.getOverlays();
Drawable herUAre = this.getResources().getDrawable(R.drawable.ic_launcher);
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(herUAre);
OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);
currentLocationOverlay.addOverlay(currentLocation);
mapOverlays.clear();
mapOverlays.add(currentLocationOverlay);
}
上面的代码用一个针显示当前位置它可以工作:-)
下面的代码我不确定如何开始工作??????
public ArrayList<OverlayItem> getLocations()
{
ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
String[] tableColumns = new String[]{"_id", "address", "description", "latitude", "longitude"};
Cursor cursor = db.query("spots", tableColumns, null, null, null, null, null);
cursor.moveToFirst();
do{
String adress = (cursor.getString(cursor.getColumnIndex("adress")));
String description = (cursor.getString(cursor.getColumnIndex("description")));
String lat1 = (cursor.getString(cursor.getColumnIndex("latitude")));
Double lat = Double.parseDouble(lat1);
String long1 = (cursor.getString(cursor.getColumnIndex("longitude")));
Double lon = Double.parseDouble(long1);
GeoPoint point = new GeoPoint((int) (lat * 1E6),(int) (lon * 1E6));
locations.add(new OverlayItem(point, adress, description));
}
while (cursor.moveToNext());
return locations;
}
// missing some code here to ad to my CurrentLocationOverlay class ore do i have to make a
//new class for this ??
这是 CurrentLocationOverlay.class
public class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public CurrentLocationOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem item){
mOverlays.add(item);
setLastFocusedIndex(-1);
populate(); // Calls the method createItem()
}
@Override
protected boolean onTap(int arg0) {
Log.d("Tapped", mOverlays.get(arg0).getSnippet());
return true;
}
}