Interactive search of places... help required.

26 views
Skip to first unread message

romanca...@gmail.com

unread,
Sep 4, 2018, 10:54:28 PM9/4/18
to CodenameOne Discussions
hi shail!

im facing a problem of performance when i tryed to implement and interactive search of addresses against google maps or openstreet.

the problem is the performance that differs so much than the native applications that uses that functionlaty, and i think that the problems is because on each character typed the function make the query with the new character but the prior querys still returning data. 

have you any idea to increase the performance? is a way of solution to kill the connectionrequest that becomes old because the new character introduced? 

thanks!

this is my code:

        ArrayList records = new ArrayList();
//        records.clear();
        //    getMarkers();

        if (text.length() > 3) {

            Map<String, Object> result;
            ArrayList tmpResult;

        try {
            ConnectionRequest r = new ConnectionRequest();
            r.setPost(false);
            r.setUrl("https://nominatim.openstreetmap.org/search.php");
            r.addArgument("q", text);
            r.addArgument("format", "json");
            r.addArgument("addressdetails", "1");
          //  r.addArgument("limit", "1");
            r.setDuplicateSupported(false);
            r.setCacheMode(ConnectionRequest.CachingMode.SMART);
            NetworkManager.getInstance().addToQueueAndWait(r);

            result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(r.getResponseData()), "UTF-8"));
            tmpResult = (ArrayList) Result.fromContent(result).getAsArray("//root").get(0);

            for (Object s : tmpResult) {
                HashMap item = ((HashMap) s);
                Place auxPlace = new Place();

                Coord newCoord = new Coord(
                        Double.valueOf(item.get("lat").toString()),
                        Double.valueOf(item.get("lon").toString())
                );

                auxPlace.setPlaceName(item.get("display_name").toString());
                auxPlace.setCoords(newCoord);
                auxPlace.setPlace_id(item.get("place_id").toString());
                records.add(auxPlace);
            }

        } catch (UnsupportedEncodingException ex) {
//                Logger.getLogger(SearchLayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            //              Logger.getLogger(SearchLayer.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IndexOutOfBoundsException ex) {
            //              Logger.getLogger(SearchLayer.class.getName()).log(Level.SEVERE, null, ex);
        }

     
        return records;
    }

Shai Almog

unread,
Sep 5, 2018, 12:34:17 AM9/5/18
to CodenameOne Discussions
Hi,
this is a problem I dealt with in the Uber clone code: https://www.amazon.com/Create-Uber-Clone-Days-mobile-ebook/dp/B07FRXZRRV/
I didn't search based on a text field event instead I set a timer. If a timer already exists you cancel it. Otherwise when it elapses a search is sent.

I also added caching to prevent duplicate requests e.g. if a user deletes a character etc.

milo roconga

unread,
Sep 5, 2018, 12:37:57 AM9/5/18
to codenameone...@googlegroups.com
Have you a sample of the code?? That technique makes the difference?

--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/BNOZb1bEUKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/203ca32f-a7f1-473d-b649-6d6796bb9772%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shai Almog

unread,
Sep 5, 2018, 11:54:35 PM9/5/18
to CodenameOne Discussions
It's in the book/course. It's not trivial and includes some moving parts.
Reply all
Reply to author
Forward
0 new messages