【发布时间】:2014-10-13 23:35:51
【问题描述】:
我正在尝试在我的应用中实现一项功能,该功能使用 Google Maps API 方向在地图中绘制路径(从 A 到 B)。由于某种原因,应用程序似乎甚至无法发出请求。
这是 logcat 报告的内容:
这是我用来做这件事的类:
public class DrawPath {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
MainActivity actividadPrincipal;
public DrawPath(MainActivity actividadPrincipal){
this.actividadPrincipal = actividadPrincipal;
}
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng( (((double) lat / 1E5)),
(((double) lng / 1E5) ));
poly.add(p);
}
return poly;
}
public void drawPath(String result) {
try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
for(int z = 0; z<list.size()-1;z++){
LatLng src= list.get(z);
LatLng dest= list.get(z+1);
Polyline line = actividadPrincipal.googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
.width(2)
.color(Color.BLUE).geodesic(true));
}
}
catch (JSONException e) {
}
}
// Parser Class
public String getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
is.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
//First of all we will get source and destination points between which we have to draw route. Then we will pass these attribute to getJSONFromUrl function.
public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json");
urlString.append("?origin="+Double.toString(sourcelat)+","+Double.toString( sourcelog));// from
urlString.append("&destination="+Double.toString(destlat)+","+Double.toString(destlog));// to
urlString.append("&sensor=false");
urlString.append("&mode=driving");
urlString.append("&alternatives=true");
return urlString.toString();
}
}
改编自Answer : Draw path between two points using Google Maps Android API v2
现在,我检查了Directions Api Documentation 并没有发现与此问题相关的任何内容。
这就是我从onCreate底部调用上述函数的方式(我还没有设计按钮)
//test
DrawPath dPath = new DrawPath(this);
String path = dPath.makeURL(markers.get(0).marker.getPosition().latitude,
markers.get(0).marker.getPosition().longitude,
markers.get(1).marker.getPosition().latitude,
markers.get(1).marker.getPosition().longitude);
dPath.drawPath(path);
我检查了链接,没有发现任何问题。也许我错过了许可?但在这种情况下它会抛出异常。
我尝试按照@3amoura 的建议将 http 更改为 https 并在请求末尾添加“&key=myApiKey”,但出现了同样的错误。
更新
在修复了一些小问题并将 URL 硬编码以请求http://maps.googleapis.com/maps/api/directions/json?origin=Granada&destination=Malaga&sensor=false 之后,它可以工作,所以问题出在 makeUrl 中,我一定是弄错了
【问题讨论】:
-
您是否从 Google 控制台生成了有关 Direction API 的 api 密钥并在您的清单文件中使用它?
-
糟糕,我不知道我需要另一个 API 密钥。现在就去检查一下。 编辑:我有一个分配给我的项目的 API 密钥,我需要一个吗?我找不到任何关于它的信息
标签: android google-maps google-maps-api-2 directions