android - Google maps Places API not working -
package com.example.googlemapstestproject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder; import java.util.arraylist; import java.util.hashmap; import java.util.list; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.os.asynctask; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.text.editable; import android.text.textwatcher; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.autocompletetextview; import android.widget.button; import android.widget.simpleadapter; import android.widget.toast; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.googlemap.onmaplongclicklistener; import com.google.android.gms.maps.googlemap.onmylocationbuttonclicklistener; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; public class mainactivity extends actionbaractivity implements onmaplongclicklistener, onmylocationbuttonclicklistener, android.view.view.onclicklistener { private googlemap mmap; button userlocation;`enter code here` gpstracker gps; placestask placestask; parsertask parsertask; autocompletetextview autocompview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); autocompview = (autocompletetextview) findviewbyid(r.id.atv_places); autocompview.setthreshold(1); autocompview.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { placestask = new placestask(); placestask.execute(s.tostring()); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); try { // loading map initilizemap(); } catch (exception e) { e.printstacktrace(); } mmap.setonmaplongclicklistener(this); mmap.setmylocationenabled(true); mmap.getuisettings().setzoomcontrolsenabled(true); } private string downloadurl(string strurl) throws ioexception { string data = ""; inputstream istream = null; httpurlconnection urlconnection = null; try { url url = new url(strurl); // creating http connection communicate url urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading data url istream = urlconnection.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(istream)); stringbuffer sb = new stringbuffer(); string line = ""; while ((line = br.readline()) != null) { sb.append(line); } data = sb.tostring(); br.close(); } catch (exception e) { log.d("exception while downloading url", e.tostring()); } { istream.close(); urlconnection.disconnect(); } return data; } // fetches places googleplaces autocomplete web service private class placestask extends asynctask<string, void, string> { @override protected string doinbackground(string... place) { // storing data web service string data = ""; // obtain browser key https://code.google.com/apis/console string key = "key=aizasydtg7d-jnrlrxe75qdeeeagr1xnshgx9v4"; string input = ""; try { input = "input=" + urlencoder.encode(place[0], "utf-8"); } catch (unsupportedencodingexception e1) { e1.printstacktrace(); } // place type searched string types = "types=(cities)"; // sensor enabled string sensor = "sensor=false"; // building parameters web service string parameters = input + "&" + types + "&" + sensor + "&" + key; // output format string output = "json"; // building url web service string url = "https://maps.googleapis.com/maps/api/place/autocomplete/" + output + "?" + parameters; try { // fetching data service data = downloadurl(url); } catch (exception e) { log.d("background task", e.tostring()); } return data; } // executed after complete execution of doinbackground() method @override protected void onpostexecute(string result) { // instantiating parsertask parses json data // geocoding webservice // in non-ui thread parsertask parsertask = new parsertask(); // start parsing places in json format // invokes "doinbackground()" method of class parsetask parsertask.execute(result); } } // class parse google places in json format private class parsertask extends asynctask<string, integer, list<hashmap<string, string>>> { jsonobject jobject; @override protected list<hashmap<string, string>> doinbackground(string... jsondata) { list<hashmap<string, string>> places = null; placejsonparser placejsonparser = new placejsonparser(); try { jobject = new jsonobject(jsondata[0]); // getting parsed data list places = placejsonparser.parse(jobject); } catch (exception e) { log.d("exception", e.tostring()); } return places; } @override protected void onpostexecute(list<hashmap<string, string>> result) { string[] = new string[] { "description" }; int[] = new int[] { r.layout.listview_layout }; // creating simpleadapter autocompletetextview simpleadapter adapter = new simpleadapter(getbasecontext(), result, android.r.layout.simple_list_item_1, from, to); // setting adapter autocompview.setadapter(adapter); } } @override public void onclick(view v) { // todo auto-generated method stub } }
updated code update, looks fine there nothing appearing still when type in place.
i have made google maps half , half listview if has solutions on how them work great.
if use library provides googleplaceautocomplete widget, check out sprockets (i'm developer). after setting api key, add working places api autocomplete layout like:
<net.sf.sprockets.widget.googleplaceautocomplete xmlns:sprockets="http://schemas.android.com/apk/res-auto" android:id="@+id/place" android:layout_width="match_parent" android:layout_height="wrap_content" sprockets:types="(cities)"/>
Comments
Post a Comment