java - 谷歌地图标记标题显示是完整列表
问题描述
我现在正在使用 google maps 项目并从 firebase 获取我的数据,我能够在该站点中所有善良的开发人员的帮助下绘制多个标记,一件事是在绘制这些标记时我的标记标题是相同的,“相同" 表示我所有的标记都有相同的标题请看我的图片
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private GoogleMap googleMap;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();
private ArrayList<String> Names = new ArrayList<>();
String Lat ,Lon,Names1;
double latitude , longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Positions");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
// System.out.println(chidSnap.getKey()); //displays the key for the node
// System.out.println(chidSnap.child("Latitude").getValue());
// System.out.println( chidSnap.child("Longitude").getValue()); //gives the value for given keyname
//some latitude and logitude value
// System.out.println(latlngs); //gives the value for given keyname
Lat = String.valueOf(chidSnap.child("Latitude").getValue());
Lon = String.valueOf(chidSnap.child("Longitude").getValue());
Names1 = String.valueOf(chidSnap.getKey());
latitude= Double.parseDouble(Lat);
longitude= Double.parseDouble(Lon);
latlngs.add(new LatLng(latitude, longitude));
Names.add(Names1);
}
if(mMap != null) {
for (LatLng point : latlngs) {
options.position(point);
options.title(String.valueOf(Names));
options.snippet("someDesc");
mMap.addMarker(options);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// System.out.println(Names);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for (LatLng point : latlngs) {
options.position(point);
options.title(String.valueOf(Names));
options.snippet("someDesc");
googleMap.addMarker(options);
}
}
}
我的 MapsActivity 上面。我期待的是一个标记应该命名为Lex B05,另一个用户应该是amanda提前感谢您的帮助!..
解决方案
那是因为您将相同的 arrayList 名称传递给所有标记,请在添加标记时尝试以下代码
for (int i=0;i< latlngs.size();i++) {
LatLng point=latlngs.get(i);
options.position(point);
options.title(Names.get(i));
options.snippet("someDesc");
mMap.addMarker(options);
}
希望这会有所帮助!
推荐阅读
- javascript - 传单弹出表单:在弹出表单提交中包含传单 latlong 值
- c# - EF6 不保存更改
- python - Python - 嵌套字典中的最大值键,泛化
- qt - 将在 Debian 下运行的带有 GLADE 文件的 Pygtk 应用程序迁移到 MS Windows10 中。如何?
- wordpress - 博客文章无法在我的自定义 WordPress 主题上打开
- ruby-on-rails - Rails:为 Mongoid 数据库创建基于下拉列表的搜索过滤器
- go - 如何在 Go 中使用中缀(比较)运算符作为参数
- stored-procedures - 从 SQL 代理作业调用时,工作存储过程失败
- python - 在散景图中表示点的 x 坐标
- docker - ENV/ARG 命令未在 Dockerfile 中填充变量