Android 如何在Google Maps API v2中将数据传递到自定义窗口信息?
我有一个自定义的地图窗口信息布局,但我看不到如何从标记传递数据的任何示例 有人能帮忙吗Android 如何在Google Maps API v2中将数据传递到自定义窗口信息?,android,google-maps-markers,google-maps-android-api-2,Android,Google Maps Markers,Google Maps Android Api 2,我有一个自定义的地图窗口信息布局,但我看不到如何从标记传递数据的任何示例 有人能帮忙吗 mMap.setInfoWindowAdapter(new InfoWindowAdapter() { // Use default InfoWindow frame @Override public View getInfoWindow(Marker arg0) { return null; } @Override public View
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = getActivity().getLayoutInflater().inflate(R.layout.info_window_layout, null);
ImageView pic = (ImageView) v.findViewById(R.id.pic);
String url = <????>;
// set pic image from url
TextView name = (TextView) v.findViewById(R.id.name);
name.setText(<????>);
TextView address = (TextView) v.findViewById(R.id.address);
address.setText(<????>);
return v;
}
});
mCenterList = MyApplication.dbHelper.getAllCenter();
for(Center center : mCenterList){
Marker marker = mMap.addMarker(
new MarkerOptions().position(new LatLng(center.lat, center.lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.elipin)));
// How can I pass custom data, here pic, name and address, to the marker, to
// make it available in getInfoContents?
}
mMap.setInfoWindowAdapter(新的InfoWindowAdapter(){
//使用默认信息窗口框架
@凌驾
公共视图getInfoWindow(标记arg0){
返回null;
}
@凌驾
公共视图getInfoContents(标记arg0){
视图v=getActivity().GetLayoutFlater().inflate(R.layout.info\u window\u layout,null);
ImageView图片=(ImageView)v.findViewById(R.id.pic);
字符串url=;
//从url设置图片
TextView name=(TextView)v.findViewById(R.id.name);
name.setText();
TextView地址=(TextView)v.findViewById(R.id.address);
address.setText();
返回v;
}
});
mCenterList=MyApplication.dbHelper.getAllCenter();
用于(中心:mCenterList){
Marker Marker=mMap.addMarker(
新标记选项().位置(新板条(center.lat,center.lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.elipin));
//我如何将自定义数据(这里是pic、名称和地址)传递给标记器和
//让它在getInfoContents中可用吗?
}
info\u window\u layout.xml
看看这个代码示例,代码中有注释供解释:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
@Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker args) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
clickMarkerLatLng = args.getPosition();
//Setting title text
TextView title = (TextView) v.findViewById(R.id.tvTitle);
title.setText(args.getTitle());
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker)
{
if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
{
if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.", Toast.LENGTH_SHORT).show();
}
else
{
FlurryAgent.onEvent("Start navigation window was clicked from daily map");
tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
for (Task tmptask : tasksRepository)
{
String tempTaskLat = String.valueOf(tmptask.getLatitude());
String tempTaskLng = String.valueOf(tmptask.getLongtitude());
Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));
if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
task = tmptask;
break;
}
}
Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
intent.putExtra(TasksListActivity.KEY_ID, task.getId());
startActivity(intent);
}
}
else
{
Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.", Toast.LENGTH_SHORT).show();
}
}
});
// Returning the view containing InfoWindow contents
return v;
}
});
更新:
然后,您应该将一个HashMap
对象与您的标记相关联,在这里查看以下答案:
因为
标记
是最终的
,所以不能从中创建子类
因此,我找到的最佳解决方案是使用标记中的某些内容(例如代码片段)来保存一个键,该键允许您查找其他数据。例如,它可能是数据模型的HashMap
的键,或者可能是数据库的主键,或者其他什么
然后,您的
不是直接来自标记
,而是来自真实的数据模型,其中标记
仅具有将两者联系在一起的标识信息。下面的方法
setOnMarkerClickListener(新的OnMarkerClickListener(){
有助于传递值
ie标记参数返回标记计数,如1、2、3等
像arraylist.get(1.details)一样传递此数字,您可以检索相应的详细信息
从列表中,我找到了这个库,并在我的项目中使用了它。它很有用,可以帮助您轻松地将数据传递到窗口中
谢谢,我解决了这个问题,我将数据存储在数组中,为infowindow创建了一个自定义布局,然后在标题后面传递数组的索引器,即:“title/indexer”。在infowindow中,我拆分字符串,并使用索引器使用数组,然后获取数据。您使用的是getPosition()和getTitle(),这是默认的标记方法。我需要传递自定义数据,例如,在我的示例中,一个图像、一个名称和一个地址(我知道我可以使用title和snippet传递名称和地址,但我希望能够添加更多数据)。请看下面关于如何将HashMap对象与标记关联的答案:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
@Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker args) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
clickMarkerLatLng = args.getPosition();
//Setting title text
TextView title = (TextView) v.findViewById(R.id.tvTitle);
title.setText(args.getTitle());
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker)
{
if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
{
if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.", Toast.LENGTH_SHORT).show();
}
else
{
FlurryAgent.onEvent("Start navigation window was clicked from daily map");
tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
for (Task tmptask : tasksRepository)
{
String tempTaskLat = String.valueOf(tmptask.getLatitude());
String tempTaskLng = String.valueOf(tmptask.getLongtitude());
Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));
if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
task = tmptask;
break;
}
}
Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
intent.putExtra(TasksListActivity.KEY_ID, task.getId());
startActivity(intent);
}
}
else
{
Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.", Toast.LENGTH_SHORT).show();
}
}
});
// Returning the view containing InfoWindow contents
return v;
}
});
@Override
public boolean onMarkerClick(**Marker arg0**) { }