问题描述
我有一个应用程序,我从数据库中获取经度和纬度,并希望在图形页面
来显示它。我能显示最近存储的人,但我觉得是需要在一些数组
循环,增加它的叠加。我不知道该怎么尽管实现它。
下面是我的buttonClick,并会经/纬度取,并在图形页面
显示它:
buttonShowMarkers =(按钮)findViewById(R.id.button_SHOW_MARKERS);buttonShowMarkers.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
尝试{
DB = openOrCreateDatabase(LocationFetch.db,MODE_PRIVATE,NULL);
光标= db.rawQuery(SELECT * FROM用户,NULL);
cursor.moveToFirst();
做{
INT DATAID = cursor.getColumnIndex(ID);
串dataString = cursor.getString(0);
Log.d(ID数据从数据库中---->中,dataString); 字符串数据名= cursor.getString(1)的ToString()修剪()。
Log.d(名数据从数据库中---->中,数据名); 串dataLAT = cursor.getString(2)的ToString()修剪();
Log.d(LAT数据从数据库中----->中,dataLAT);
INT纬度= cursor.getColumnIndex(纬度); 串dataLON = cursor.getString(3)的ToString()修剪();
Log.d(LON数据从数据库中----->中,dataLON);
INT经度= cursor.getColumnIndex(经度); showLatLon(纬度,经度,数据名);
// ArrayList的< OverlayItem>项目=新的ArrayList< OverlayItem>();
/ *列表与LT;叠加> ListOverlays =调用MapView.getOverlays();
* OverlayItem [] = markerItem新{OverlayItem(新的GeoPoint(经度,纬度),+数据名称,+ dataString)};
* OverlayItem markerItem =新OverlayItem(新的GeoPoint((int)的(纬度* 1E6),(INT)(经度* 1E6)),+数据名,+ dataString);
* drawableOne = getResources()getDrawable(R.drawable.location_blue)。
* helloItemizedOverlay =新HelloItemizedOverlay(drawableOne);
* helloItemizedOverlay.addOverlayItem(markerItem);
*调用MapView.getOverlays()加(helloItemizedOverlay)。
* ListOverlays.add(myItemizedOverlay); * / //为(INT I = DATAID;)
}而(cursor.moveToNext()); / *的String [] = arrLat新的String [] {} LATdata;
的String [] = arrLon新的String [] {} LONdata; * / // showLatLon(dataLAT,dataLON);
cursor.moveToNext();
}赶上(的SQLException E){
e.printStackTrace();
} {最后
cursor.close();
}
db.close();
} 私人无效showLatLon(INT纬度,经度INT,字符串nAMEdata){
// GeoPoint的点=新的GeoPoint((纬度),(纬度)); 清单<&叠加GT; ListOverlays =调用MapView.getOverlays(); // OverlayItem [] = markerItem新{OverlayItem(新的GeoPoint(经度,纬度),+数据名称,+ dataString)};
OverlayItem markerItem =新OverlayItem(新的GeoPoint(纬度,纬度),+ nAMEdata,NULL);
drawableOne = getResources()getDrawable(R.drawable.location_blue)。 helloItemizedOverlay =新HelloItemizedOverlay(drawableOne);
helloItemizedOverlay.addOverlayItem(markerItem); 调用MapView.getOverlays()加(helloItemizedOverlay)。 ListOverlays.add(myItemizedOverlay);
}
});
您可以尝试使用这个解决方案 -
- 创建一个名为类的 LatLonPoint - 这基本上是执行转换从latlon到的GeoPoint -
公共类LatLonPoint扩展的GeoPoint {
公共LatLonPoint(双纬度,经度双){
超级((int)的(纬度* 1E6),(INT)(经度* 1E6));
}
}
创建一个分项叠加 -
公共类HelloItemizedOverlay扩展ItemizedOverlay< OverlayItem> {
私人的ArrayList< OverlayItem> mOverlays =新的ArrayList< OverlayItem>();
私人语境mContext;公共HelloItemizedOverlay(可绘制defaultMarker,上下文的背景下){
超(boundCenterBottom(defaultMarker));
mContext =背景;
}公共无效addOverlay(OverlayItem叠加){
mOverlays.add(覆盖);
填充();
}@覆盖
保护OverlayItem createItem中(int i)以{
返回mOverlays.get(ⅰ);
}@覆盖
公众诠释大小(){
返回mOverlays.size();
}@覆盖
保护布尔中的onTap(INT指数){
OverlayItem项目= mOverlays.get(指数);
AlertDialog.Builder对话框=新AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
返回true;
}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件,MapView类MapView类){ 如果(键code == KeyEvent.KEY code_BACK){ }
返回super.onKeyDown(键code,事件的MapView);
}
}
现在的主要活动 -
{尝试
DB = openOrCreateDatabase(LocationFetch.db,MODE_PRIVATE,NULL);
光标= db.rawQuery(SELECT * FROM用户,NULL);
cursor.moveToFirst(); 做{
INT DATAID = cursor.getColumnIndex(ID);
串dataString = cursor.getString(0);
Log.d(ID数据从数据库中---->中,dataString); 字符串数据名= cursor.getString(1)的ToString()修剪()。
Log.d(名数据从数据库中---->中,数据名); 串dataLAT = cursor.getString(2)的ToString()修剪();
Log.d(LAT数据从数据库中----->中,dataLAT);
INT纬度= cursor.getColumnIndex(纬度); 串dataLON = cursor.getString(3)的ToString()修剪();
Log.d(LON数据从数据库中----->中,dataLON);
INT经度= cursor.getColumnIndex(经度); //此处添加的项目 -
itemizedoverlay.addOverlay(新OverlayItem(新LatLonPoint(纬度,经度),数据名,NULL));
// showLatLon(纬度,经度,数据名);
// ArrayList的< OverlayItem>项目=新的ArrayList< OverlayItem>();
/ *列表与LT;叠加> ListOverlays =调用MapView.getOverlays();
* OverlayItem [] = markerItem新{OverlayItem(新的GeoPoint(经度,纬度),+数据名称,+ dataString)};
* OverlayItem markerItem =新OverlayItem(新的GeoPoint((int)的(纬度* 1E6),(INT)(经度* 1E6)),+数据名,+ dataString);
* drawableOne = getResources()getDrawable(R.drawable.location_blue)。
* helloItemizedOverlay =新HelloItemizedOverlay(drawableOne);
* helloItemizedOverlay.addOverlayItem(markerItem);
*调用MapView.getOverlays()加(helloItemizedOverlay)。
* ListOverlays.add(myItemizedOverlay); * / //为(INT I = DATAID;)
}而(cursor.moveToNext()); //此处添加逐项叠加
mapOverlays.add(itemizedoverlay);
/ *的String [] = arrLat新的String [] {} LATdata;
的String [] = arrLon新的String [] {} LONdata; * / // showLatLon(dataLAT,dataLON);
//cursor.moveToNext();
}赶上(的SQLException E){
e.printStackTrace();
} {最后
cursor.close();
}
有关,你必须初始化这两个项目beffore上述块在做..而 -
列表<&叠加GT; mapOverlays = mMapView.getOverlays();
和
HelloItemizedOverlay itemizedoverlay =新HelloItemizedOverlay(
绘制,这一点);
EDIT--添加迭代光标部分 -
可绘制可绘制= this.getResources()。getDrawable(
R.drawable.ic_launcher);
清单<&叠加GT; mapOverlays = mMapView.getOverlays();
HelloItemizedOverlay itemizedoverlay =新HelloItemizedOverlay(
绘制,这一点);
而(cursor.moveToNext())
{
//获取值 -
INT DATAID = cursor.getColumnIndex(ID); 串dataString = cursor.getString(0);
Log.d(ID数据从数据库中---->中,dataString); 字符串数据名= cursor.getString(1)的ToString()修剪()。
Log.d(名数据从数据库中---->中,数据名); 串dataLAT = cursor.getString(2)的ToString()修剪();
Log.d(LAT数据从数据库中----->中,dataLAT); INT纬度= cursor.getColumnIndex(纬度);
串dataLON = cursor.getString(3)的ToString()修剪();
Log.d(LON数据从数据库中----->中,dataLON);
INT经度= cursor.getColumnIndex(经度); //假设你的逻辑检索值是正确的
//添加到逐项叠加这里
itemizedoverlay.addOverlay(新OverlayItem(新LatLonPoint(纬度,经度),数据名,NULL)); }
mapOverlays.add(itemizedoverlay);
I have an application where I fetch longitude and latitude from my database and want to display it in a MapView
. I am able to show the one stored most recently, but I think some array is needed in a for
loop to add it in an overlay. I don't know how to implement it though.
Here is my buttonClick on which it will fetch lat/long and display it in a MapView
:
buttonShowMarkers = (Button) findViewById(R.id.button_SHOW_MARKERS);
buttonShowMarkers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
db = openOrCreateDatabase("LocationFetch.db", MODE_PRIVATE , null);
cursor = db.rawQuery("SELECT * FROM user" , null);
cursor.moveToFirst();
do {
int dataID = cursor.getColumnIndex("id");
String dataString = cursor.getString(0);
Log.d("ID DATA FROM DATABASE---->" , dataString);
String dataNAME = cursor.getString(1).toString().trim();
Log.d("NAME DATA FROM DATABASE---->", dataNAME);
String dataLAT = cursor.getString(2).toString().trim();
Log.d("LAT DATA FROM DATABASE----->" , dataLAT);
int latitude = cursor.getColumnIndex("latitude");
String dataLON = cursor.getString(3).toString().trim();
Log.d("LON DATA FROM DATABASE----->" , dataLON);
int longitude = cursor.getColumnIndex("longitude");
showLatLon(latitude , longitude , dataNAME);
// ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
/* List<Overlay> ListOverlays = mapView.getOverlays();
* OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)};
* OverlayItem markerItem = new OverlayItem(new GeoPoint((int)(latitude *1E6), (int)(longitude *1E6)) , ""+dataNAME , ""+dataString);
* drawableOne = getResources().getDrawable(R.drawable.location_blue);
* helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);
* helloItemizedOverlay.addOverlayItem(markerItem);
* mapView.getOverlays().add(helloItemizedOverlay);
* ListOverlays.add(myItemizedOverlay);*/
//for(int i = dataID ; )
} while (cursor.moveToNext());
/*String[] arrLat = new String[]{LATdata};
String[] arrLon = new String[]{LONdata};*/
//showLatLon(dataLAT , dataLON);
cursor.moveToNext();
} catch(SQLException e) {
e.printStackTrace();
} finally {
cursor.close();
}
db.close();
}
private void showLatLon(int latitude , int longitude , String nAMEdata) {
// GeoPoint point = new GeoPoint( (latitude) , (latitude) );
List<Overlay> ListOverlays = mapView.getOverlays();
//OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)};
OverlayItem markerItem = new OverlayItem(new GeoPoint(latitude,latitude ) , ""+nAMEdata , null);
drawableOne = getResources().getDrawable(R.drawable.location_blue);
helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);
helloItemizedOverlay.addOverlayItem(markerItem);
mapView.getOverlays().add(helloItemizedOverlay);
ListOverlays.add(myItemizedOverlay);
}
});
You can try using this solution -
- Create a class named LatLonPoint - this basically does the conversion from latlon to GeoPoint -
public class LatLonPoint extends GeoPoint {
public LatLonPoint(double latitude, double longitude) {
super((int) (latitude * 1E6), (int) (longitude * 1E6));
}
}
Create a Itemized Overlay -
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event, MapView mapView) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event, mapView);
}
}
Now in the main activity -
try {
db = openOrCreateDatabase("LocationFetch.db", MODE_PRIVATE , null);
cursor = db.rawQuery("SELECT * FROM user" , null);
cursor.moveToFirst();
do {
int dataID = cursor.getColumnIndex("id");
String dataString = cursor.getString(0);
Log.d("ID DATA FROM DATABASE---->" , dataString);
String dataNAME = cursor.getString(1).toString().trim();
Log.d("NAME DATA FROM DATABASE---->", dataNAME);
String dataLAT = cursor.getString(2).toString().trim();
Log.d("LAT DATA FROM DATABASE----->" , dataLAT);
int latitude = cursor.getColumnIndex("latitude");
String dataLON = cursor.getString(3).toString().trim();
Log.d("LON DATA FROM DATABASE----->" , dataLON);
int longitude = cursor.getColumnIndex("longitude");
// Add the item here --
itemizedoverlay.addOverlay(new OverlayItem(new LatLonPoint(latitude, longitude), dataNAME, null));
//showLatLon(latitude , longitude , dataNAME);
// ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
/* List<Overlay> ListOverlays = mapView.getOverlays();
* OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)};
* OverlayItem markerItem = new OverlayItem(new GeoPoint((int)(latitude *1E6), (int)(longitude *1E6)) , ""+dataNAME , ""+dataString);
* drawableOne = getResources().getDrawable(R.drawable.location_blue);
* helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);
* helloItemizedOverlay.addOverlayItem(markerItem);
* mapView.getOverlays().add(helloItemizedOverlay);
* ListOverlays.add(myItemizedOverlay);*/
//for(int i = dataID ; )
} while (cursor.moveToNext());
// Add all itemized overlay here
mapOverlays.add(itemizedoverlay);
/*String[] arrLat = new String[]{LATdata};
String[] arrLon = new String[]{LONdata};*/
//showLatLon(dataLAT , dataLON);
//cursor.moveToNext();
} catch(SQLException e) {
e.printStackTrace();
} finally {
cursor.close();
}
For the above block you have to initialize these two items beffore the do.. while -
List<Overlay> mapOverlays = mMapView.getOverlays();
and
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
EDIT-- Adding iterating Cursor Part -
Drawable drawable = this.getResources().getDrawable(
R.drawable.ic_launcher);
List<Overlay> mapOverlays = mMapView.getOverlays();
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
while(cursor.moveToNext())
{
// get the values --
int dataID = cursor.getColumnIndex("id");
String dataString = cursor.getString(0);
Log.d("ID DATA FROM DATABASE---->" , dataString);
String dataNAME = cursor.getString(1).toString().trim();
Log.d("NAME DATA FROM DATABASE---->", dataNAME);
String dataLAT = cursor.getString(2).toString().trim();
Log.d("LAT DATA FROM DATABASE----->" , dataLAT);
int latitude = cursor.getColumnIndex("latitude");
String dataLON = cursor.getString(3).toString().trim();
Log.d("LON DATA FROM DATABASE----->" , dataLON);
int longitude = cursor.getColumnIndex("longitude");
// Assuming your logic for retrieving values is correct
// Add into the itemized Overlay here
itemizedoverlay.addOverlay(new OverlayItem(new LatLonPoint(latitude, longitude), dataNAME, null));
}
mapOverlays.add(itemizedoverlay);
这篇关于从数据库中检索位置,并在MapView显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!