google distance matrix with autocomplete on android -
i trying header saying, create app google distance matrix autocomplete on android. far, research , studying on books myself, difficult come code , reach out here. far have gone
https://developers.google.com/maps/documentation/distancematrix/#introduction
and google directions document
http://www.claytical.com/blog/android-dynamic-autocompletion-using-google-places-api http://www.stackoverflow.com/questions/9142885/geocoder-autocomplete-in-android http://www.stackoverflow.com/questions/6456090/android-google-map-finding-distance/6456161#6456161
all these links have not helped me understand , understand concept of requesting api address , returning json , have "parse" json , extract necessary information, distance in case. not how can create autocomplete has suggestion google maps distance matrix , how can send origin , destination accordingly. frustrating , have claytical.com.
here's main.java
package com.example.autocomplete; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.url; import java.net.urlconnection; import java.net.urlencoder; import java.util.arraylist; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.content.context; import android.os.asynctask; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.util.log; import android.widget.arrayadapter; import android.widget.autocompletetextview; public class main extends activity { /** called when activity first created. */ public arrayadapter<string> adapter; public autocompletetextview textview; public object s; class getplaces extends asynctask<string, void, arraylist<string>> { public arrayadapter<string> adapter; public autocompletetextview textview; object s; @override // 3 dots java array of strings protected arraylist<string> doinbackground(string... args) { log.d("gottago", "doinbackground"); arraylist<string> predictionsarr = new arraylist<string>(); try { url googleplaces = new url( // urlencoder.encode(url,"utf-8"); "https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ urlencoder.encode(s.tostring(), "utf-8") +"&types=geocode&language=en&sensor=true&key=<yourapikeygoeshere>"); urlconnection tc = googleplaces.openconnection(); bufferedreader in = new bufferedreader(new inputstreamreader( tc.getinputstream())); string line; stringbuffer sb = new stringbuffer(); //take google's legible json , turn 1 big string. while ((line = in.readline()) != null) { sb.append(line); } //turn string json object jsonobject predictions = new jsonobject(sb.tostring()); //now json array that's inside object jsonarray ja = new jsonarray(predictions.getstring("predictions")); (int = 0; < ja.length(); i++) { jsonobject jo = (jsonobject) ja.get(i); //add each entry our array predictionsarr.add(jo.getstring("description")); } } catch (ioexception e) { log.e("yourapp", "getplaces : doinbackground", e); } catch (jsonexception e) { log.e("yourapp", "getplaces : doinbackground", e); } return predictionsarr; } //then our post @override protected void onpostexecute(arraylist<string> result) { log.d("yourapp", "onpostexecute : " + result.size()); //update adapter arrayadapter<string> adapter = new arrayadapter<string>(getbasecontext(), r.layout.item_list); adapter.setnotifyonchange(true); //attach adapter textview textview.setadapter(adapter); (string string : result) { log.d("yourapp", "onpostexecute : result = " + string); adapter.add(string); adapter.notifydatasetchanged(); } log.d("yourapp", "onpostexecute : autocompleteadapter" + adapter.getcount()); } private context getbasecontext() { // todo auto-generated method stub return null; } } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); final arrayadapter<string> adapter = new arrayadapter<string>(this,r.layout.item_list); final autocompletetextview textview = (autocompletetextview) findviewbyid(r.id.autocompletetextview1); adapter.setnotifyonchange(true); textview.setadapter(adapter); textview.addtextchangedlistener(new textwatcher() { public void ontextchanged(charsequence s, int start, int before, int count) { if (count%3 == 1) { adapter.clear(); getplaces task = new getplaces(); //now pass argument in textview task task.execute(textview.gettext().tostring()); } } public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } public void aftertextchanged(editable s) { } }); } }
and main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#dddddd"> <autocompletetextview android:id="@+id/autocompletetextview1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margintop="10dp" > <requestfocus></requestfocus> </autocompletetextview>
lastly, here's logcat requested.
05-14 17:41:38.163: e/trace(1390): error opening trace file: no such file or directory (2) 05-14 17:41:39.323: d/gralloc_goldfish(1390): emulator without gpu emulation detected. 05-14 17:41:45.284: d/gottago(1390): doinbackground 05-14 17:41:45.296: w/dalvikvm(1390): threadid=11: thread exiting uncaught exception (group=0x40a13300) 05-14 17:41:45.314: e/androidruntime(1390): fatal exception: asynctask #1 05-14 17:41:45.314: e/androidruntime(1390): java.lang.runtimeexception: error occured while executing doinbackground() 05-14 17:41:45.314: e/androidruntime(1390): @ android.os.asynctask$3.done(asynctask.java:299) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.futuretask.run(futuretask.java:137) 05-14 17:41:45.314: e/androidruntime(1390): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 05-14 17:41:45.314: e/androidruntime(1390): @ java.lang.thread.run(thread.java:856) 05-14 17:41:45.314: e/androidruntime(1390): caused by: java.lang.nullpointerexception 05-14 17:41:45.314: e/androidruntime(1390): @ com.example.autocomplete.main$getplaces.doinbackground(main.java:54) 05-14 17:41:45.314: e/androidruntime(1390): @ com.example.autocomplete.main$getplaces.doinbackground(main.java:1) 05-14 17:41:45.314: e/androidruntime(1390): @ android.os.asynctask$2.call(asynctask.java:287) 05-14 17:41:45.314: e/androidruntime(1390): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 05-14 17:41:45.314: e/androidruntime(1390): ... 5 more 05-14 17:41:47.584: i/choreographer(1390): skipped 31 frames! application may doing work on main thread. 05-14 17:41:47.904: i/process(1390): sending signal. pid: 1390 sig: 9
it launches fine , brings auto complete textview type letter, crashes. reading far , thank in advance help!
ok first thing url malformed:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + urlencoder.encode(s.tostring(), "utf-8") + "&types=geocode&language=en&sensor=true&key=<yourapikeygoeshere>
you need replace <yourapikeygoeshere>
api key work. next thing how form json array. need replace
jsonarray ja = new jsonarray(predictions.getstring("predictions"));
with
jsonarray ja = new jsonarray(predictions.getjsonarray("predictions"));
next in loop, replace
jsonobject jo = (jsonobject) ja.get(i);
with
jsonobject jo = ja.getjsonobject(i);
try , let me know how goes.
Comments
Post a Comment