Android 自定义信息窗口,谷歌地图
我有一个应用程序,它可以从数据库中显示地图上的一些位置。一切正常,但我不想在我的自定义信息窗口中显示评级栏。我尝试过一些教程,但问题是我使用php从JSON获取数据。它可以工作,但默认情况下,评级栏是从数据库检索到的最后一个信息 这是我的类,实现了Android 自定义信息窗口,谷歌地图,android,google-maps,Android,Google Maps,我有一个应用程序,它可以从数据库中显示地图上的一些位置。一切正常,但我不想在我的自定义信息窗口中显示评级栏。我尝试过一些教程,但问题是我使用php从JSON获取数据。它可以工作,但默认情况下,评级栏是从数据库检索到的最后一个信息 这是我的类,实现了GoogleMap.InfoWindowAdapter public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter { private Activity co
GoogleMap.InfoWindowAdapter
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Activity context;
private int rating;
private RatingBar RTB;
public CustomInfoWindowAdapter(Activity context,int rating){
this.context = context;
this.rating=rating;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View view = context.getLayoutInflater().inflate(R.layout.customwindow, null);
RTB = (RatingBar) view.findViewById(R.id.mark_rating);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvSubTitle = (TextView) view.findViewById(R.id.tv_subtitle);
RTB.setRating(rating);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
return view;
}
这是我添加标记的地方
for(int i=0 ; i<response.length();i++){
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("nom");
String long_i = person.getString("longitude");
String lat_i = person.getString("latitude");
int rating = person.getInt("rating");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(lat_i) , Double.parseDouble(long_i)))
.title(name)
.snippet("Nothing")
.icon(BitmapDescriptorFactory
.fromBitmap(resizeMapIcons("doctor_icon",85,85))));
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MapsActivity.this,rating);
mMap.setInfoWindowAdapter(adapter);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.6850,90.3563), 6.0f));
}
对于(int i=0;i问题您不能在标记上设置任何其他内容位图描述符
解决方案这是将任何xml布局转换为位图的方法。您可以将布局转换为位图
这里是魔术
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(bitmap));
marker.setIcon(icon);
及
和layout\u marker\u view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_marker_view"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/ic_marker_blue"
android:gravity="center"
android:padding="4dp">
<com.baselib.view.CircleImageView
android:id="@+id/civProfile"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
</LinearLayout>
无需在适配器中传递评级,只需设置一次适配器。此外,将相机放置一次,因为在循环中它具有相同的效果。对于每个人的单独评级,使用setTag()将对象与标记关联,如下所示:
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MapsActivity.this);
mMap.setInfoWindowAdapter(adapter);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.6850,90.3563), 6.0f));
// associate objects with markers
for(int i=0 ; i<response.length();i++){
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("nom");
String long_i = person.getString("longitude");
String lat_i = person.getString("latitude");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(lat_i) , Double.parseDouble(long_i)))
.title(name)
.snippet("Nothing")
.icon(BitmapDescriptorFactory
.fromBitmap(resizeMapIcons("doctor_icon",85,85)))).setTag(person);
}
您在自定义信息窗口中面临的具体问题是什么?谢谢,但我想您还没有理解我的问题。我想做的只是从数据库中检索每个用户的评级,并将其添加到评级栏。正如您在图片中所看到的,它会获取最后一个评级并将其设置为所有评级栏,直到我没有获取您的评级为止,您是否会这样做没有可触摸的分级栏?没有。正如你在屏幕上看到的,我在“自定义信息”窗口中已经有了分级栏,但问题是。它对所有标记显示相同的分级。
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MapsActivity.this);
mMap.setInfoWindowAdapter(adapter);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.6850,90.3563), 6.0f));
// associate objects with markers
for(int i=0 ; i<response.length();i++){
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("nom");
String long_i = person.getString("longitude");
String lat_i = person.getString("latitude");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(lat_i) , Double.parseDouble(long_i)))
.title(name)
.snippet("Nothing")
.icon(BitmapDescriptorFactory
.fromBitmap(resizeMapIcons("doctor_icon",85,85)))).setTag(person);
}
@Override
public View getInfoContents(Marker marker) {
View view = context.getLayoutInflater().inflate(R.layout.customwindow, null);
RTB = (RatingBar) view.findViewById(R.id.mark_rating);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvSubTitle = (TextView) view.findViewById(R.id.tv_subtitle);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
// get json object associated with it:
JSONObject person = (JSONObject) marker.getTag();
// get rating:
int rating = person.getInt("rating");
// set it
if (rating!= null){
RTB.setRating(rating)
}
return view;
}