java - No error in my app of finding nearby places from my application. but not showing nearby places -
i new android working on google map , google places api. have been searching last 1 month. not success. m developing app can find nearest hospitals/laboratories his/her location. please me identify problem. i'll grateful.
here manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.naqvi.myapplication"> <!-- access_coarse/fine_location permissions not required use google maps android api v2, must specify either coarse or fine location permissions 'mylocation' functionality. --> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="in.wptrafficanalyzer.locationgeocodingv2.permission.maps_receive" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="com.javapapers.android.googleplacesdetail.permission.maps_receive" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="@string/google_maps_key" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <!-- api key google maps-based apis defined string resource. (see file "res/values/google_maps_api.xml"). note api key linked encryption key used sign apk. need different api key each encryption key, including release key used sign apk publishing. can define keys debug , release targets in src/debug/ , src/release/. --> <meta-data android:name="com.google.android.geo.api_key" android:value="@string/google_maps_key" /> <activity android:name=".mapsactivity" android:label="@string/title_activity_maps"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
main mapsactivity
public class mapsactivity extends fragmentactivity implements locationlistener { private static final string google_api_key = "aizasyavpsjjj_1pbmsx1pheyzq6exwvw3-5pwm"; googlemap googlemap; double latitude = 0; double longitude = 0; edittext placetext; private int proximity_radius = 5000; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //show error dialog if goolgleplayservices not available if (!isgoogleplayservicesavailable()) { finish(); } setcontentview(r.layout.activity_maps); placetext = (edittext) findviewbyid(r.id.actv_search); button btn_find = (button) findviewbyid(r.id.btn_find); supportmapfragment fragment = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map); googlemap = fragment.getmap(); if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { // todo: consider calling // activitycompat#requestpermissions // here request missing permissions, , overriding // public void onrequestpermissionsresult(int requestcode, string[] permissions, // int[] grantresults) // handle case user grants permission. see documentation // activitycompat#requestpermissions more details. return; } googlemap.setmylocationenabled(true); locationmanager locationmanager = (locationmanager) getsystemservice(location_service); criteria criteria = new criteria(); string bestprovider = locationmanager.getbestprovider(criteria, true); location location = locationmanager.getlastknownlocation(bestprovider); if (location != null) { onlocationchanged(location); } locationmanager.requestlocationupdates(bestprovider, 20000, 0, this); btn_find.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string type = placetext.gettext().tostring(); stringbuilder googleplacesurl = new stringbuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); googleplacesurl.append("location=" + latitude + "," + longitude); googleplacesurl.append("&radius=" + proximity_radius); googleplacesurl.append("&types=" + type); googleplacesurl.append("&sensor=true"); googleplacesurl.append("&key=" + google_api_key); googleplacesreadtask googleplacesreadtask = new googleplacesreadtask(); object[] topass = new object[2]; topass[0] = googlemap; topass[1] = googleplacesurl.tostring(); googleplacesreadtask.execute(topass); } }); } private boolean isgoogleplayservicesavailable() { int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if (connectionresult.success == status) { return true; } else { googleplayservicesutil.geterrordialog(status, this, 0).show(); return false; } } @override public void onlocationchanged(location location) { latitude = location.getlatitude(); longitude = location.getlongitude(); latlng latlng = new latlng(latitude, longitude); googlemap.movecamera(cameraupdatefactory.newlatlng(latlng)); googlemap.animatecamera(cameraupdatefactory.zoomto(12)); } @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 } }
this googleplacereadtask class
public class googleplacesreadtask extends asynctask<object, integer, string> { string googleplacesdata = null; googlemap googlemap; @override protected string doinbackground(object... inputobj) { try { googlemap = (googlemap) inputobj[0]; string googleplacesurl = (string) inputobj[1]; http http = new http(); googleplacesdata = http.read(googleplacesurl); } catch (exception e) { log.d("google place read task", e.tostring()); } return googleplacesdata; } @override protected void onpostexecute(string result) { placesdisplaytask placesdisplaytask = new placesdisplaytask(); object[] topass = new object[2]; topass[0] = googlemap; topass[1] = result; placesdisplaytask.execute(topass); } }
this http
class
public class http { public string read(string httpurl) throws ioexception { string httpdata = ""; inputstream inputstream = null; httpurlconnection httpurlconnection = null; try { url url = new url(httpurl); httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.connect(); inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); stringbuffer stringbuffer = new stringbuffer(); string line = ""; while ((line = bufferedreader.readline()) != null) { stringbuffer.append(line); } httpdata = stringbuffer.tostring(); bufferedreader.close(); } catch (exception e) { log.d("exception - reading http url", e.tostring()); } { inputstream.close(); httpurlconnection.disconnect(); } return httpdata; } }
this places
class
public class places { public list<hashmap<string, string>> parse(jsonobject jsonobject) { jsonarray jsonarray = null; try { jsonarray = jsonobject.getjsonarray("results"); } catch (jsonexception e) { e.printstacktrace(); } return getplaces(jsonarray); } private list<hashmap<string, string>> getplaces(jsonarray jsonarray) { int placescount = jsonarray.length(); list<hashmap<string, string>> placeslist = new arraylist<hashmap<string, string>>(); hashmap<string, string> placemap = null; (int = 0; < placescount; i++) { try { placemap = getplace((jsonobject) jsonarray.get(i)); placeslist.add(placemap); } catch (jsonexception e) { e.printstacktrace(); } } return placeslist; } private hashmap<string, string> getplace(jsonobject googleplacejson) { hashmap<string, string> googleplacemap = new hashmap<string, string>(); string placename = "-na-"; string vicinity = "-na-"; string latitude = ""; string longitude = ""; string reference = ""; try { if (!googleplacejson.isnull("name")) { placename = googleplacejson.getstring("name"); } if (!googleplacejson.isnull("vicinity")) { vicinity = googleplacejson.getstring("vicinity"); } latitude = googleplacejson.getjsonobject("geometry").getjsonobject("location").getstring("lat"); longitude = googleplacejson.getjsonobject("geometry").getjsonobject("location").getstring("lng"); reference = googleplacejson.getstring("reference"); googleplacemap.put("place_name", placename); googleplacemap.put("vicinity", vicinity); googleplacemap.put("lat", latitude); googleplacemap.put("lng", longitude); googleplacemap.put("reference", reference); } catch (jsonexception e) { e.printstacktrace(); } return googleplacemap; } }
at last placesdisplaytask
class
public class placesdisplaytask extends asynctask>> {
jsonobject googleplacesjson; googlemap googlemap; @override protected list<hashmap<string, string>> doinbackground(object... inputobj) { list<hashmap<string, string>> googleplaceslist = null; places placejsonparser = new places(); try { googlemap = (googlemap) inputobj[0]; googleplacesjson = new jsonobject((string) inputobj[1]); googleplaceslist = placejsonparser.parse(googleplacesjson); } catch (exception e) { log.d("exception", e.tostring()); } return googleplaceslist; } @override protected void onpostexecute(list<hashmap<string, string>> list) { googlemap.clear(); (int = 0; < list.size(); i++) { markeroptions markeroptions = new markeroptions(); hashmap<string, string> googleplace = list.get(i); double lat = double.parsedouble(googleplace.get("lat")); double lng = double.parsedouble(googleplace.get("lng")); string placename = googleplace.get("place_name"); string vicinity = googleplace.get("vicinity"); latlng latlng = new latlng(lat, lng); markeroptions.position(latlng); markeroptions.title(placename + " : " + vicinity); googlemap.addmarker(markeroptions); } } }
Comments
Post a Comment