android - How to get id of Marker clicked in Google Map inside setOnInfoWindowClickListener -
here trying set id inside doinbackground , call in setoninfowindowclicklistener.but problem is getting id of first element in list. want id of marker clicked. using variable private static string id; store id product.id=productjson.getstring("id"); id=product.id; assign it.
googlemap.setoninfowindowclicklistener(new oninfowindowclicklistener() { @override public void oninfowindowclick(marker arg0) { intent.putextra("product_id",id); system.out.println(id); startactivity(intent); } how can id ondoinbackground , use in intent.putextra().
public class frnt_mapactivity extends activity { jsonarray jsonarray3; // google map private static googlemap googlemap; private static string id; private intent intent; hashmap<marker, integer> hashmap=new hashmap<marker, integer>(); public static latlng latlong ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.frnt_map_activity); googlemap=((mapfragment)getfragmentmanager().findfragmentbyid(r.id.places_map)).getmap(); googlemap.setmaptype(googlemap.map_type_normal); googlemap.setmylocationenabled(true); googlemap.getuisettings().setzoomcontrolsenabled(true); googlemap.getuisettings().setmylocationbuttonenabled(true); googlemap.getuisettings().setcompassenabled(true); googlemap.getuisettings().setrotategesturesenabled(true); googlemap.getuisettings().setzoomgesturesenabled(true); googlemap.setmylocationenabled(true); latlng coordinate = new latlng(50.85514, 0.58382); cameraupdate yourlocation = cameraupdatefactory.newlatlngzoom(coordinate, 18); googlemap.animatecamera(yourlocation); new frnt_micons_activity().execute(); new locationlist().execute(); new lookingforlist().execute(); createproductlisttask responsedownloadtask = new createproductlisttask("https://www.towncitycards.com/webservice_action.php?action=all_products"); responsedownloadtask.execute(); intent=new intent(frnt_mapactivity.this,mainactivity.class); googlemap.setoninfowindowclicklistener(new oninfowindowclicklistener() { @override public void oninfowindowclick(marker arg0) { intent.putextra("product_id",id); system.out.println(id); startactivity(intent); } }); protected class createproductlisttask extends asynctask<void, void, list<product>> { private string serverurl; public createproductlisttask(string url) { super(); this.serverurl = url; } @override protected list<product> doinbackground(void... params) { hostnameverifier hostnameverifier = org.apache.http.conn.ssl.sslsocketfactory.allow_all_hostname_verifier; defaulthttpclient client = new defaulthttpclient(); schemeregistry registry = new schemeregistry(); sslsocketfactory socketfactory = sslsocketfactory.getsocketfactory(); socketfactory.sethostnameverifier((x509hostnameverifier) hostnameverifier); registry.register(new scheme("http", socketfactory, 443)); singleclientconnmanager mgr = new singleclientconnmanager(client.getparams(), registry); defaulthttpclient httpclient = new defaulthttpclient(mgr, client.getparams()); // set verifier httpsurlconnection.setdefaulthostnameverifier(hostnameverifier); httppost httppost = new httppost("http://towncitycards.com/webservice_action.php?action=all_products"); urlconnection urlconn = null; bufferedreader bufferedreader = null; try { url url = new url(this.serverurl); urlconn = url.openconnection(); bufferedreader = new bufferedreader(new inputstreamreader(urlconn.getinputstream())); stringbuffer stringbuffer = new stringbuffer(); string line; while ((line = bufferedreader.readline()) != null) { stringbuffer.append(line); } jsonobject response = new jsonobject(stringbuffer.tostring()); list<product> products = new arraylist<>(); hashmap<string, bitmap> iconsmap = new hashmap<>(); try { jsonarray productsjson = response.getjsonarray("all_products"); (int ixproduct=0; ixproduct<productsjson.length(); ixproduct++) { jsonobject productjson = productsjson.getjsonobject(ixproduct); string mapiconstr = productjson.getstring("map_icon"); uri uri = new uri(mapiconstr); string[] segments = uri.getpath().split("/"); string iconname = segments[segments.length-1]; // percetn-encode url string mapiconpath = mapiconstr.substring(0, mapiconstr.indexof(iconname)); string iconurlstring = mapiconpath + urlencoder.encode(iconname, "utf-8"); // replace "http:" "https:" iconurlstring = iconurlstring.replace("http:", "https:"); bitmap bmp; if (!iconsmap.containskey(iconurlstring)) { bmp = getbitmapfromurl(iconurlstring); // populate map unique icons iconsmap.put(iconurlstring, bmp); } else { bmp = iconsmap.get(iconurlstring); } if (bmp != null) { try { product product = new product(); product.id=productjson.getstring("id"); product.name = productjson.getstring("post_title"); product.lat = productjson.getdouble("latitude"); product.lon = productjson.getdouble("longitude"); id=product.id; bmp = bitmap.createscaledbitmap(bmp,(int)(bmp.getwidth()*3), (int)(bmp.getheight()*3), true); product.icon = bmp; products.add(product); } catch (exception ignore) { } } } } catch (jsonexception ex) { log.e("app", "failure", ex); } return products; } catch(exception ex) { log.e("app", "yourdatatask", ex); return null; } { if(bufferedreader != null) { try { bufferedreader.close(); } catch (ioexception e) { e.printstacktrace(); } } } } @override protected void onpostexecute(list<product> products) { if(products != null) { (final product product : products) { googlemap.addmarker(new markeroptions() .position(new latlng(product.lat, product.lon)) .title(product.name) .icon(bitmapdescriptorfactory.frombitmap(product.icon)) ); } } } } public static bitmap getbitmapfromurl(string src) { try { url url = new url(src); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.insamplesize = 1; bmoptions.injustdecodebounds = false; bitmap mybitmap = bitmapfactory.decodestream(input, null, bmoptions); return mybitmap; } catch (ioexception e) { return null; } } }
add product.id tag marker:
marker marker = googlemap.addmarker(new markeroptions() .position(new latlng(product.lat, product.lon)) .title(product.name) .icon(bitmapdescriptorfactory.frombitmap(product.icon))); marker.settag(product.id); and on oninfowindowclicklistener tag:
googlemap.setoninfowindowclicklistener(new oninfowindowclicklistener() { @override public void oninfowindowclick(marker marker) { intent.putextra("product_id",(string) marker.gettag()); startactivity(intent); } } another option keep map<marker, string> store id associated each marker:
private map<marker, string> markerids = new hashmap<>(); you need create markers , store them this:
marker marker = googlemap.addmarker(new markeroptions().position(markerposition).title("my marker")); markerids.put(marker, product.id); and on oninfowindowclicklistener tag:
googlemap.setoninfowindowclicklistener(new oninfowindowclicklistener() { @override public void oninfowindowclick(marker marker) { intent.putextra("product_id",markerids.get(marker)); startactivity(intent); } }
Comments
Post a Comment