Created
December 30, 2016 20:51
-
-
Save ahmedengu/9bfa7bfbae3037d651d829e2e98aa276 to your computer and use it in GitHub Desktop.
Google Maps AutoComplete Test codenameone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.myapp; // change to your package | |
import com.codename1.io.ConnectionRequest; | |
import com.codename1.io.JSONParser; | |
import com.codename1.io.NetworkManager; | |
import com.codename1.ui.*; | |
import com.codename1.ui.layouts.BoxLayout; | |
import com.codename1.ui.plaf.UIManager; | |
import com.codename1.ui.util.Resources; | |
import com.codename1.util.Callback; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
public class GoogleMapsAutoCompleteTest { | |
private static final String MAPS_KEY = "Your_Key"; // Your maps key here | |
private Form current; | |
public void init(Object context) { | |
try { | |
Resources theme = Resources.openLayered("/theme"); | |
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0])); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void start() { | |
if (current != null) { | |
current.show(); | |
return; | |
} | |
Form hi = new Form("Maps AutoComplete Test"); | |
hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); | |
TextField textField = new TextField(""); | |
textField.setHint("Address"); | |
hi.add(textField); | |
Button address = new Button("Get AutoComplete"); | |
address.addActionListener(evt -> { | |
String text = textField.getText(); | |
ArrayList<String> list = getAutoComplete(textField.getText(), "en", "address"); | |
for (int i = 0; i < list.size(); i++) { | |
hi.add(new Label(list.get(i))); | |
} | |
hi.revalidate(); | |
}); | |
hi.add(address); | |
hi.show(); | |
} | |
public void stop() { | |
current = Display.getInstance().getCurrent(); | |
} | |
public void destroy() { | |
} | |
public static ArrayList<String> getAutoComplete(String input, String language, String types) { | |
ArrayList<String> ret = new ArrayList<>(); | |
try { | |
ConnectionRequest request = new ConnectionRequest("https://maps.googleapis.com/maps/api/place/autocomplete/json", false); | |
request.addArgument("key", MAPS_KEY); | |
request.addArgument("input", input); | |
request.addArgument("types", types); | |
request.addArgument("language", language); | |
NetworkManager.getInstance().addToQueueAndWait(request); | |
Map<String, Object> response = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(request.getResponseData()), "UTF-8")); | |
if (response.get("predictions") != null) { | |
ArrayList results = (ArrayList) response.get("predictions"); | |
if (results.size() > 0) | |
for (int i = 0; i < results.size(); i++) { | |
ret.add((String) ((LinkedHashMap) results.get(i)).get("description")); | |
} | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return ret; | |
} | |
public static void getAutoCompleteAsync(String input, String language, String types, Callback callback) { | |
ConnectionRequest request = new ConnectionRequest("https://maps.googleapis.com/maps/api/place/autocomplete/json", false) { | |
@Override | |
protected void readResponse(InputStream input) throws IOException { | |
ArrayList<String> ret = new ArrayList<>(); | |
Map<String, Object> response = new JSONParser().parseJSON(new InputStreamReader(input, "UTF-8")); | |
if (response.get("predictions") != null) { | |
ArrayList results = (ArrayList) response.get("predictions"); | |
if (results.size() > 0) | |
for (int i = 0; i < results.size(); i++) { | |
ret.add((String) ((LinkedHashMap) results.get(i)).get("description")); | |
} | |
} | |
callback.onSucess(ret); | |
} | |
}; | |
request.addArgument("key", MAPS_KEY); | |
request.addArgument("input", input); | |
request.addArgument("types", types); | |
request.addArgument("language", language); | |
NetworkManager.getInstance().addToQueue(request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment