How to Create 2 Marker with different Info Window in Google Maps Android? -
i wonderin can create multiple marker different info window, in google maps v2 in android, in case right here, try 3 different marker, 1. school marker 2. direction (fromposition) 3. direction (toposition)
note : nama_sma >> school name : nama_bemo >> taxi
right now, can showing marker, problem each marker have same info window, xml :
<com.example.frontend.search.mapwrapperlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map_relative_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/map" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/layout1" class="com.google.android.gms.maps.supportmapfragment"/> </com.example.frontend.search.mapwrapperlayout>
nb : mapwrapperlayout wrapper customizing info window in apps
this activity
public class map_result extends fragmentactivity { googlemap map; private dbdatasource datasource; private viewgroup infowindow; private textview infotitle; private textview infosnippet; private button infobutton2; //this class adding button in info window private oninfowindowelemtouchlistener infobuttonlistener2; latlng querypoint; latlng querypoint_jalur, querypoint_jalur2; //nama_bemo == taxi string nama_bemo=""; string latitude_asal=""; string longitude_asal=""; string latitude_tujuan=""; string longitude_tujuan=""; string temp_jalur=""; string temp_jalur2=""; double lat_jalur=0.0; double lon_jalur=0.0; double lat_jalur2=0.0; double lon_jalur2=0.0; string lat, lon; string temp, nama_sma; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.map); datasource = = new dbdatasource(this); //make database open datasource.open(); map = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap(); this.infowindow = (viewgroup)getlayoutinflater().inflate(r.layout.custom_info_window, null); this.infotitle = (textview)infowindow.findviewbyid(r.id.title); this.infosnippet = (textview)infowindow.findviewbyid(r.id.snippet); this.infobutton2 = (button)infowindow.findviewbyid(r.id.button2); this.infobuttonlistener2 = new oninfowindowelemtouchlistener(infobutton2, getresources().getdrawable(r.drawable.custom_btn_beige), getresources().getdrawable(r.drawable.custom_btn_orange)) { @override protected void onclickconfirmed(view v, marker marker) { // todo auto-generated method stub } } }; this.infobutton2.setontouchlistener(infobuttonlistener2); //method see map seemap(); private void seemap() { final mapwrapperlayout mapwrapperlayout = (mapwrapperlayout)findviewbyid(r.id.map_relative_layout); //an array list getting 2 point (fromposition) , (toposition) taxi arraylist<sma> list_jalur = datasource.getallpoint_jalur_sma(id_sma); (int i2 = 0; i2 < list_jalur.size(); i2++) { nama_bemo = list_jalur.get(i2).getnama_bemo(); latitude_asal = list_jalur.get(i2).getlat_asal_bemo(); longitude_asal = list_jalur.get(i2).getlon_asal_bemo(); latitude_tujuan = list_jalur.get(i2).getlat_tujuan_bemo(); longitude_tujuan = list_jalur.get(i2).getlon_tujuan_bemo(); temp_jalur = latitude_asal + longitude_asal; temp_jalur2 = latitude_tujuan + longitude_tujuan; string[] splitcoordinate_jalur = temp_jalur.split(", "); lat_jalur = double.valueof(splitcoordinate_jalur[0]); lon_jalur = double.valueof(splitcoordinate_jalur[1]); //this latlng (from position) querypoint_jalur = new latlng(lat_jalur, lon_jalur); string[] splitcoordinate_jalur2 = temp_jalur2.split(", "); lat_jalur2 = double.valueof(splitcoordinate_jalur2[0]); lon_jalur2 = double.valueof(splitcoordinate_jalur2[1]); //this latlng (from toposition) querypoint_jalur2 = new latlng(lat_jalur2, lon_jalur2); //if type of taxi if(nama_bemo.equals("a")) { map.addpolyline(new polylineoptions() .add(querypoint_jalur, querypoint_jalur2) .width(7) .color(color.red)); // question right here, until here, //i can create marker in map, marker //have 1 same info windows. in case, in code below, //i try make marker different infowindow map.addmarker(new markeroptions() .position(querypoint_jalur2) .icon(bitmapdescriptorfactory.fromresource(r.drawable.pin_smk))); map.addmarker(new markeroptions() .position(querypoint_jalur) .icon(bitmapdescriptorfactory.fromresource(r.drawable.pin_smk))); } } //this loop getting school data (int = 0; < this.list.size(); i++) { //school name nama_sma = list.get(0).getnama().tostring(); lat = list.get(i).getlatitude().tostring(); lon = list.get(i).getlongitude().tostring(); temp = lat + lon; string[] splitcoordinate = temp.split(", "); = double.valueof(splitcoordinate[0]); b = double.valueof(splitcoordinate[1]); //this marker of school position querypoint = new latlng(a, b); map.setmylocationenabled(true); map.movecamera(cameraupdatefactory.newlatlngzoom(querypoint,13)); //this is setup custom info window map.setinfowindowadapter(new infowindowadapter() { @override public view getinfowindow(marker marker) { // todo auto-generated method stub infotitle.settext(marker.gettitle()); infosnippet.settext(marker.getsnippet()); infobuttonlistener2.setmarker(marker); mapwrapperlayout.setmarkerwithinfowindow(marker, infowindow); return infowindow; } @override public view getinfocontents(marker marker) { // todo auto-generated method stub return null; } }); map.addmarker(new markeroptions() .position(querypoint) .title(nama_sma) }
the problem is, 3 marker (fromposition), (toposition), , (school marker) showing same info window, can me? create different info window in each marker? needed. thank before
edit: if want have different info window each marker, have create view each 1 of them. create map info windows of markers , use marker id key, can check if created view.
try this:
private map<string,viewgroup> infowindows = new hashmap<string,viewgroup>(); ... @override public view getinfowindow(marker marker) { // or create infowindows here instead of in oncreate viewgroup infowindow = infowindows.get(marker.getid()); if(infowindow==null){ //if info window not yet created marker, create infowindow = (viewgroup)getlayoutinflater().inflate(r.layout.custom_info_window, null); button infobutton = (button)infowindow.findviewbyid(r.id.button2); infobutton.setontouchlistener(infobuttonlistener2); infowindows.put(marker.getid(), infowindow); } // set or update texts ((textview)infowindow.findviewbyid(r.id.title)).settext(marker.gettitle()); ((textview)infowindow.findviewbyid(r.id.snippet)).settext(marker.getsnippet()); //why need set marker listener? infobuttonlistener2.setmarker(marker); mapwrapperlayout.setmarkerwithinfowindow(marker, infowindow); return infowindow; }
Comments
Post a Comment