android - Google Maps application works on emulator but not real device -
i new user here , reputation not enough display images of app along question make more elaborative unfortunately asking question without images of app
i using google nexus 4 ..api 18 (android version 4.3) virtual device , had been developing app run on android device having api 11 or 11+ , have test app on real android device (samsung galaxy s2 lite) facing problems are
1) whenever user enter location name in edit text box , click on find button in same activity marker drawn on map below.. works fine in virtual device every time on android device @ point when ever user clicks on find button application crash.
2) in second activity here have made button location when ever user clicks on ..it gps coordinates of device , reverse geocode , display location adress in 1 of edit text boxes .this works fine in virtual device .it take moment every thing in real device not display thing when ever click on icon @ top left shows search using gps not display thing why taking time here .. these 2 problems facing on android device these working on virtual device..
plzzz me there working on fyp , hang here
the code problem no 1 here follow
package com.example.citytourguide; import java.io.ioexception; import java.util.list; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; import android.content.context; import android.content.intent; import android.location.address; import android.location.geocoder; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class tourguide extends fragmentactivity { googlemap googlemap; marker mark; latlng latlng; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_tour_guide); supportmapfragment supportmapfragment = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map); googlemap = supportmapfragment.getmap(); button btn_route=(button) findviewbyid(r.id.route); btn_route.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub startactivity(new intent(getbasecontext(),showpath.class)); } }); // getting reference btn_find of layout activity_main button btn_find = (button) findviewbyid(r.id.btn_find); btn_find.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { edittext loc_name = (edittext) findviewbyid(r.id.edittext1); string location = loc_name.gettext().tostring(); if(location.equals("")){ toast.maketext(getbasecontext(), "please! enter find...", toast.length_short).show(); } else{ connectivitymanager connmgr = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo networkinfo = connmgr.getactivenetworkinfo(); boolean iswifi = networkinfo.gettype() == connectivitymanager.type_wifi; if (iswifi==true) { new geocodertask().execute(location); } else { toast.maketext(getbasecontext(), "ooops! error in network connection...", toast.length_long).show(); } } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.tour_guide, menu); return true; } // asynctask class accessing geocoding web service protected class geocodertask extends asynctask<string, void, list<address>>{ @override protected list<address> doinbackground(string... locationname) { // creating instance of geocoder classtry geocoder geocoder = new geocoder(getbasecontext()); list<address> addresses = null; try { // getting maximum of 3 address matches input text addresses = geocoder.getfromlocationname(locationname[0], 3); } catch (ioexception e) { e.printstacktrace(); } return addresses; } @override protected void onpostexecute(list<address> addresses) { if(addresses==null || addresses.size()==0){ toast.maketext(getbasecontext(), "no location found", toast.length_short).show(); } // clears existing markers on map googlemap.clear(); // adding markers on google map each matching address for(int i=0;i<addresses.size();i++){ address address = (address) addresses.get(i); // creating instance of geopoint, display in google map latlng = new latlng(address.getlatitude(), address.getlongitude()); string addresstext = string.format("%s, %s", address.getmaxaddresslineindex() > 0 ? address.getaddressline(0) : "",address.getcountryname()); // showing marker on map marker mark =googlemap.addmarker(new markeroptions().position(latlng).title(addresstext)); mark.showinfowindow(); // displaying title on marker // locate first location if(i==0) googlemap.animatecamera(cameraupdatefactory.newlatlng(latlng)); } } } }
this file have access location
package com.example.citytourguide; import java.io.ioexception; import java.util.list; import com.google.android.gms.maps.model.latlng; import android.location.address; import android.location.criteria; import android.location.geocoder; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.asynctask; import android.os.bundle; import android.provider.settings; import android.app.alertdialog; import android.app.dialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.support.v4.app.fragmentactivity; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.autocompletetextview; import android.widget.button; import android.widget.toast; public class showpath extends fragmentactivity implements onclicklistener,locationlistener { autocompletetextview auto_to, auto_from; button btn_path, btn_loc; double lat,lng; int = 1 ; int = 1 ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_show_path); // getting reference btn_find of layout activity_main auto_from = (autocompletetextview) findviewbyid(r.id.from); auto_to = (autocompletetextview) findviewbyid(r.id.to); auto_from.settext(null); auto_to.settext(null); btn_path=(button) findviewbyid(r.id.path); btn_loc=(button) findviewbyid(r.id.btn_loc); btn_path.setonclicklistener(this); btn_loc.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.show_path, menu); menuitem menuitem=menu.add(menu.none,menu.first,menu.none,"save"); menuitem.setshowasaction(menuitem.show_as_action_if_room); return true; } public boolean onmenuitemselected(int featureid,menuitem item) { toast.maketext(getbasecontext(),"map has been concluded " + " " , toast.length_short).show(); return super.onmenuitemselected(featureid, item); } @override public void onclick(view v) { // todo auto-generated method stub switch(v.getid()){ case r.id.path: string from=auto_from.gettext().tostring(); string = auto_to.gettext().tostring(); if(to.equals("")&& from.equals("")){ toast.maketext(getbasecontext(), "please! enter start , destination properly...", toast.length_short).show(); } else{ connectivitymanager connmgr = (connectivitymanager) getsystemservice(connectivity_service); networkinfo networkinfo = connmgr.getactivenetworkinfo(); boolean iswifi = networkinfo.gettype() == connectivitymanager.type_wifi; if (iswifi==true) { intent in = new intent(showpath.this,showmap.class); in.putextra("start", from); in.putextra("dest", to); startactivity(in); } else { toast.maketext(getbasecontext(), "ooops! error in network connection...", toast.length_long).show(); } } break; case r.id.btn_loc: // getting locationmanager object system service location_service locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); // creating criteria object retrieve provider criteria criteria = new criteria(); // getting name of best provider string provider = locationmanager.getbestprovider(criteria, true); location location = null ; if(provider.contains("gps")) { // getting current location gps if(auto_from.gettext().tostring().matches("") && (auto_to.gettext().tostring().matches(""))){ i=0 ; } else if (auto_to.gettext().tostring().matches("")) { a=0; } location = locationmanager.getlastknownlocation(provider); if(location!=null) onlocationchanged(location); } else { showdialog(0); } locationmanager.requestlocationupdates(provider, 1000, 0, this); //locationmanager.requestlocationupdates(provider, 20000, 0, this); break; default: break; } //switch statement ends } // on click func ends @override protected dialog oncreatedialog(int id){ final string message = "enable either gps or other location" + " service find current location. click ok go to" + " location services settings let so."; return new alertdialog.builder(this).setmessage(message) .settitle("warning") .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int whichbutton) { intent intent = new intent(settings.action_location_source_settings); startactivity(intent); } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int whichbutton) { toast.maketext(getbasecontext(), "gps services still disabled", toast.length_short).show(); } }).create(); } @override public void onlocationchanged(location location) { // todo auto-generated method stub lat = location.getlatitude(); lng = location.getlongitude(); latlng point = new latlng(lat,lng); // converting user location address , assign autocomplete text box new reversegeocodingtask(getbasecontext()).execute(point); } private class reversegeocodingtask extends asynctask<latlng, void, string>{ context mcontext; public reversegeocodingtask(context context){ super(); mcontext = context; } // finding address using reverse geocoding @override protected string doinbackground(latlng... params) { geocoder geocoder = new geocoder(mcontext); double latitude = params[0].latitude; double longitude = params[0].longitude; list<address> addresses = null; string addresstext=""; try { addresses = geocoder.getfromlocation(latitude, longitude,1); } catch (ioexception e) { e.printstacktrace(); } if(addresses != null && addresses.size() > 0 ){ address address = addresses.get(0); addresstext = string.format("%s, %s, %s", address.getmaxaddresslineindex() > 0 ? address.getaddressline(0) : "", address.getlocality(), address.getcountryname()); } return addresstext; } @override protected void onpostexecute(string addresstext) { if(i==0){ auto_from.settext(addresstext); i++ ; } else if(a==0){ log.d("asim","" + a); auto_to.settext(addresstext); a++; } }// post execute finish }// class reverse geocoding finish @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
in second activity real device needs time fetch location (2-10 mins) depending if in open air or in house. emulater takes location coordinates using emulater control in ddms so, won't take more seconds.try testing ur app in open air.
Comments
Post a Comment