-
-
Save ahmedengu/a76a9dc7b8c2426989ee791d59945503 to your computer and use it in GitHub Desktop.
codenameone draw routes between two locations using google directions api, sync, async , codenameone-google-maps
This file contains 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 | |
// goto line 48 | |
import com.codename1.googlemaps.MapContainer; | |
import com.codename1.io.ConnectionRequest; | |
import com.codename1.io.JSONParser; | |
import com.codename1.io.NetworkManager; | |
import com.codename1.maps.Coord; | |
import com.codename1.maps.providers.GoogleMapsProvider; | |
import com.codename1.ui.Display; | |
import com.codename1.ui.FontImage; | |
import com.codename1.ui.Form; | |
import com.codename1.ui.layouts.BorderLayout; | |
import com.codename1.ui.plaf.Style; | |
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 GoogleMapsTestApp { | |
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("Native Maps Test"); | |
hi.setLayout(new BorderLayout()); | |
final MapContainer cnt = new MapContainer(new GoogleMapsProvider(MAPS_KEY)); | |
hi.addComponent(BorderLayout.CENTER, cnt); | |
cnt.setCameraPosition(new Coord(31.2001, 29.9187)); | |
// HERE | |
Coord src = new Coord(31.2001, 29.9187); | |
Coord dest = new Coord(30.0444, 31.2357); | |
// get the routes using google directions api | |
String encoded = getRoutesEncoded(src, dest); | |
// decode the routes in an arry of coords | |
Coord[] coords = decode(encoded); | |
cnt.addPath(coords); | |
// if you wanna get it async ... | |
// getRoutesEncodedAsync(src, dest, new Callback() { | |
// @Override | |
// public void onError(Object sender, Throwable err, int errorCode, String errorMessage) { | |
// | |
// } | |
// | |
// @Override | |
// public void onSucess(Object encoded) { | |
// Coord[] coords = decode((String) encoded); | |
// cnt.addPath(coords); | |
// } | |
// }); | |
Style s = new Style(); | |
s.setBgTransparency(0); | |
s.setFgColor(0x007700); | |
cnt.addMarker(FontImage.createMaterial(FontImage.MATERIAL_LOCATION_ON, s).toEncodedImage(), src, "", "", null); | |
cnt.addMarker(FontImage.createMaterial(FontImage.MATERIAL_LOCATION_ON, s).toEncodedImage(), dest, "", "", null); | |
hi.show(); | |
} | |
public void stop() { | |
current = Display.getInstance().getCurrent(); | |
} | |
public void destroy() { | |
} | |
// src: https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java | |
public static Coord[] decode(final String encodedPath) { | |
int len = encodedPath.length(); | |
final ArrayList<Coord> path = new ArrayList<Coord>(); | |
int index = 0; | |
int lat = 0; | |
int lng = 0; | |
while (index < len) { | |
int result = 1; | |
int shift = 0; | |
int b; | |
do { | |
b = encodedPath.charAt(index++) - 63 - 1; | |
result += b << shift; | |
shift += 5; | |
} while (b >= 0x1f); | |
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); | |
result = 1; | |
shift = 0; | |
do { | |
b = encodedPath.charAt(index++) - 63 - 1; | |
result += b << shift; | |
shift += 5; | |
} while (b >= 0x1f); | |
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); | |
path.add(new Coord(lat * 1e-5, lng * 1e-5)); | |
} | |
Coord[] p = new Coord[path.size()]; | |
for (int i = 0; i < path.size(); i++) { | |
p[i] = path.get(i); | |
} | |
return p; | |
} | |
public static String getRoutesEncoded(Coord src, Coord dest) { | |
String ret = ""; | |
try { | |
ConnectionRequest request = new ConnectionRequest("https://maps.googleapis.com/maps/api/directions/json", false); | |
request.addArgument("key", MAPS_KEY); | |
request.addArgument("origin", src.getLatitude() + "," + src.getLongitude()); | |
request.addArgument("destination", dest.getLatitude() + "," + dest.getLongitude()); | |
NetworkManager.getInstance().addToQueueAndWait(request); | |
Map<String, Object> response = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(request.getResponseData()), "UTF-8")); | |
if (response.get("routes") != null) { | |
ArrayList routes = (ArrayList) response.get("routes"); | |
if (routes.size() > 0) | |
ret = ((LinkedHashMap) ((LinkedHashMap) ((ArrayList) response.get("routes")).get(0)).get("overview_polyline")).get("points").toString(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return ret; | |
} | |
public static void getRoutesEncodedAsync(Coord src, Coord dest, Callback callback) { | |
ConnectionRequest request = new ConnectionRequest("https://maps.googleapis.com/maps/api/directions/json", false) { | |
@Override | |
protected void readResponse(InputStream input) throws IOException { | |
String ret = ""; | |
Map<String, Object> response = new JSONParser().parseJSON(new InputStreamReader(input, "UTF-8")); | |
if (response.get("routes") != null) { | |
ArrayList routes = (ArrayList) response.get("routes"); | |
if (routes.size() > 0) | |
ret = ((LinkedHashMap) ((LinkedHashMap) ((ArrayList) response.get("routes")).get(0)).get("overview_polyline")).get("points").toString(); | |
} | |
callback.onSucess(ret); | |
} | |
}; | |
request.addArgument("key", MAPS_KEY); | |
request.addArgument("origin", src.getLatitude() + "," + src.getLongitude()); | |
request.addArgument("destination", dest.getLatitude() + "," + dest.getLongitude()); | |
NetworkManager.getInstance().addToQueue(request); | |
} | |
} |
All these lines gives error in my code,i am new in android development please help sir....
import com.codename1.googlemaps.MapContainer;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.JSONParser;
import com.codename1.io.NetworkManager;
import com.codename1.maps.Coord;
import com.codename1.maps.providers.GoogleMapsProvider;
import com.codename1.ui.Display;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.util.Callback;
My runs perfectly but no routes or lines are drawn between these points
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
com.g_ara.gara.controller.MapController
is a local class that i didn't notice that is there when i created the gist, However i fixed it now.Really sorry for the inconvenience and the late reply (github didn't notify me)!
Best Regards,
Ahmed