android 地图显示了多个标记(来自 sqlite 的 LatLng)
android maps displayed multiple markers (LatLng from sqlite)
我在我的 android 应用程序中使用 google 地图获取(用户)设备位置(GPS 位置),并插入数据库 (SQLite) 经纬度和地址!
现在我想用从数据库读取的 LatLng 显示多个位置...创建标记没问题,但在标记信息(国家、城市...)中只显示所有标记的最后插入位置!
这是我的代码:
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 table 的 toast 表单 rowId,并显示 3 行 ID:1、2、3 但在标记信息中显示最后一个 ID(ID 号:3)
这是我的片段:
谢谢
对于你的情况我有很多解决方案但是首先:
你的 setInfoWindowAdapter
方法失败了,它只是 调用了一次 ,所以在你迭代数据库项目并通过 drawMarker
传递信息后,它只显示了最后修改(保存)来自变量的数据,所以我建议将它移动到你的 for 循环中(我知道这不是一个完美的解决方案):
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);
..........
@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;
......
}
第二个解决方案 通过您的数据库使用 Cursor
并在任何地方使用它(这会很棒)。
第 3 使用 Clustering
Algorithm in google_maps-utils-example.
仅显示最后一个标记的信息 window,因为 setInfoWindowAdapter()
为整个地图设置了信息 window。在 setInfoWindowAdapter()
中,您需要将标记参数与相应的数据相关联。
您需要维护一个标记到数据映射。
Map<Marker, Place> markerToPlaceMap = new HashMap<>();
其中,Place
是一个 class,用于保存城市、地点和评级。
class Place {
public String city, place;
public float rating;
}
注意:请将成员更改为私有并根据您的适用性实现 getter 和 setter。
接下来,您的drawMarker()
将更改如下。它需要添加 marker
并且它与 place
相关到 markerToPlace
映射。
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()
并访问与marker
相关的Place
以设置相应的信息内容。
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;
}
});
i 在您的数据库中使用 Cursor 并获取数据行(并在 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 ...)
public View getInfoContents(Marker arg0)
(每个标记都是唯一的)然后按 id select 数据 where id = arg0
我在我的 android 应用程序中使用 google 地图获取(用户)设备位置(GPS 位置),并插入数据库 (SQLite) 经纬度和地址!
现在我想用从数据库读取的 LatLng 显示多个位置...创建标记没问题,但在标记信息(国家、城市...)中只显示所有标记的最后插入位置!
这是我的代码:
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 table 的 toast 表单 rowId,并显示 3 行 ID:1、2、3 但在标记信息中显示最后一个 ID(ID 号:3)
这是我的片段:
谢谢
对于你的情况我有很多解决方案但是首先:
你的 setInfoWindowAdapter
方法失败了,它只是 调用了一次 ,所以在你迭代数据库项目并通过 drawMarker
传递信息后,它只显示了最后修改(保存)来自变量的数据,所以我建议将它移动到你的 for 循环中(我知道这不是一个完美的解决方案):
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);
..........
@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;
......
}
第二个解决方案 通过您的数据库使用 Cursor
并在任何地方使用它(这会很棒)。
第 3 使用 Clustering
Algorithm in google_maps-utils-example.
仅显示最后一个标记的信息 window,因为 setInfoWindowAdapter()
为整个地图设置了信息 window。在 setInfoWindowAdapter()
中,您需要将标记参数与相应的数据相关联。
您需要维护一个标记到数据映射。
Map<Marker, Place> markerToPlaceMap = new HashMap<>();
其中,
Place
是一个 class,用于保存城市、地点和评级。class Place { public String city, place; public float rating; }
注意:请将成员更改为私有并根据您的适用性实现 getter 和 setter。
接下来,您的
drawMarker()
将更改如下。它需要添加marker
并且它与place
相关到markerToPlace
映射。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()
并访问与marker
相关的Place
以设置相应的信息内容。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; } });
i 在您的数据库中使用 Cursor 并获取数据行(并在 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 ...)
public View getInfoContents(Marker arg0)
(每个标记都是唯一的)然后按 id select 数据 where id = arg0