android地图显示多个标记(来自sqlite的LatLng)
在我的android应用程序中,我使用带有get(用户)设备位置(GPS位置)的谷歌地图,并将纬度、经度和地址插入到数据库(SQLite)中 现在我想显示多个位置,从数据库读取LatLng。。。创建标记时没有问题,但在标记信息(国家/地区、城市…)中,仅显示所有标记的上次插入位置 这是我的代码:android地图显示多个标记(来自sqlite的LatLng),android,sqlite,google-maps-markers,android-maps,Android,Sqlite,Google Maps Markers,Android Maps,在我的android应用程序中,我使用带有get(用户)设备位置(GPS位置)的谷歌地图,并将纬度、经度和地址插入到数据库(SQLite)中 现在我想显示多个位置,从数据库读取LatLng。。。创建标记时没有问题,但在标记信息(国家/地区、城市…)中,仅显示所有标记的上次插入位置 这是我的代码: private void displayMultiplePoint() { if (LOCATION_TABLE.size() > 0) { for (in
private void displayMultiplePoint() {
if (LOCATION_TABLE.size() > 0) {
for (int i = 0; LOCATION_TABLE.size() > i; i++) {
int id = LOCATION_TABLE.get(i).getId();
lat = LOCATION_TABLE.get(i).getLatitude();
lng = LOCATION_TABLE.get(i).getLongitude();
place = LOCATION_TABLE.get(i).getPlace();
rate = LOCATION_TABLE.get(i).getRate();
drawMarker(new LatLng(lat, lng), "city", place, rate);
displayToast(id + "" + place);
}
}
}
private void drawMarker(final LatLng point, final String city, final String place, final float rate) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = null;
try {
v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
map_image.setImageResource(R.drawable.runhd);
TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
city_txt.setText(city);
TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
place_txt.setText(place);
RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
rate_bar.setRating(rate);
} catch (Exception ev) {
System.out.print(ev.getMessage());
}
return v;
}
});
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(point);
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 6));
}
我在数据库中显示lcation表中的toast表单rowId,并显示3行id:1、2、3,但在标记信息中显示最后一个id(id号:3)
这是我的片段:
谢谢对于您的案例,我有很多解决方案,但首先: 您的fall with
setInfoWindowAdapter
方法只调用了一次,因此在您迭代数据库项并通过drawMarker
传递信息之后,它只显示了变量中最后修改(保存)的数据,因此我建议将其移动到for循环中(我知道这不是一个完美的解决方案):
第二个解决方案通过您的数据库使用,并在任何地方使用(这将非常棒)
3rd使用中的算法。仅显示最后一个标记的信息窗口,因为
setInfoWindowAdapter()
为整个地图设置信息窗口。在setInfoWindowAdapter()
中,您需要将标记参数与相应的数据相关联
Map<Marker, Place> markerToPlaceMap = new HashMap<>();
注意:请将成员更改为private,并根据您的适合性实施getter和setterdrawMarker()
将更改如下。它需要添加标记
,并将其与位置
关联到标记平面
地图
private void drawMarker(final LatLng point, final String city, final String place, final float rate) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(point);
Marker marker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 6));
markerToPlaceMap.put(marker, new Place(city, place, rating));
}
GoogleMap.setInfoWindowAdapter()
并访问与标记相关的位置
,以设置相应的信息内容
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = null;
try {
v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
map_image.setImageResource(R.drawable.runhd);
TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
city_txt.setText(markerToPlace.get(marker).city); // <- Check how corresponding place for a marker is fetched and used
TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
place_txt.setText(markerToPlace.get(marker).place);
RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
rate_bar.setRating(markerToPlace.get(marker).rate);
} catch (Exception ev) {
System.out.print(ev.getMessage());
}
return v;
}
});
mMap.setInfoWindowAdapter(新的GoogleMap.InfoWindowAdapter(){
@凌驾
公共视图getInfoWindow(标记器){
返回null;
}
@凌驾
公共视图getInfoContents(标记器){
视图v=null;
试一试{
v=getLayoutFlater().充气(R.layout.custom\u info\u contents,null);
ImageView地图\图像=(ImageView)v.findViewById(R.id.maps\图像);
map_image.setImageResource(R.drawable.runhd);
TextView city_txt=(TextView)v.findViewById(R.id.maps_city);
city_txt.setText(markerToPlace.get(marker.city);//我使用光标浏览数据库并获取数据行(并显示在Toast中)
但是
我的代码更改为:
private void displayMultiplePoint() {
Cursor cursor = DB_HELPER.LOCATION_MARKERS();
if (cursor.moveToFirst()) {
for (int i = 0; cursor.getCount() > i; i++) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
double lat = cursor.getDouble(cursor.getColumnIndex("latitude"));
double lng = cursor.getDouble(cursor.getColumnIndex("longitude"));
String place = cursor.getString(cursor.getColumnIndex("place"));
float rate = cursor.getFloat(cursor.getColumnIndex("rate"));
displayToast(id + ", " + place + rate);
cursor.moveToNext();
drawMarker(new LatLng(lat, lng));
}
cursor.close();
}
}
我得到arg0.getId表单:(m0,m1,m2…)
(对于每个标记都是唯一的)然后按id选择数据,其中id=arg0关于“在数据库中使用光标并在任何地方使用它的解决方案(这将非常棒)。”您有一个example@ghasemdeh超级简单,只需像文档一样实现它,并调用您需要的任何数据,无需out for循环。请仔细阅读
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = null;
try {
v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
map_image.setImageResource(R.drawable.runhd);
TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
city_txt.setText(markerToPlace.get(marker).city); // <- Check how corresponding place for a marker is fetched and used
TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
place_txt.setText(markerToPlace.get(marker).place);
RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
rate_bar.setRating(markerToPlace.get(marker).rate);
} catch (Exception ev) {
System.out.print(ev.getMessage());
}
return v;
}
});
private void displayMultiplePoint() {
Cursor cursor = DB_HELPER.LOCATION_MARKERS();
if (cursor.moveToFirst()) {
for (int i = 0; cursor.getCount() > i; i++) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
double lat = cursor.getDouble(cursor.getColumnIndex("latitude"));
double lng = cursor.getDouble(cursor.getColumnIndex("longitude"));
String place = cursor.getString(cursor.getColumnIndex("place"));
float rate = cursor.getFloat(cursor.getColumnIndex("rate"));
displayToast(id + ", " + place + rate);
cursor.moveToNext();
drawMarker(new LatLng(lat, lng));
}
cursor.close();
}
}
public View getInfoContents(Marker arg0)