您好我是Android的新手。我从stackoverflow中获取了一个代码,以便在谷歌地图上的两个标记点之间建立路径。但是在跑步之后我没有得到这条路。请帮忙。 我在这里使用了Google map api密钥。
我的网址是。
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=");// from
urlString.append(Double.toString(sourcelat));
urlString.append(",");
urlString
.append(Double.toString( sourcelog));
urlString.append("&destination=");// to
urlString
.append(Double.toString( destlat));
urlString.append(",");
urlString.append(Double.toString( destlog));
urlString.append("&sensor=false&mode=walking&alternatives=true");
urlString.append("&key=Google_Map_Key");
return urlString.toString();
}
这是我的绘制函数,它从JSON中获取数据。
public void drawPath(String result){
try {
//Tranform the string into a json object
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);
/* Polyline line = mMap.addPolyline(new PolylineOptions()
//.addAll(list)
.add(list.get(0))
.add(list.get(1))
.width(5)
.color(Color.parseColor("#05b1fb"))//Google maps blue color
.geodesic(true)
.visible(true)
);*/
/*line.isVisible();*/
for(int z = 0; z<list.size()-1;z++){
LatLng src= list.get(z);
LatLng dest= list.get(z+1);
Polyline line = mMap.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) {
}
}
我的JSONPArser类是
公共类JSONParser {
//static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
//public JSONParser() {
//}
public JSONParser() {
}
public String getJSONFromUrl(String url) {
try{
URL Url = new URL(url);//use a proper url instead of onlineUrl
HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
//connection.setRequestProperty("User-Agent", "");
//connection.setRequestMethod("GET");//we can use POST instead of GET method also.
//connection.setDoInput(true);
connection.connect();
//URL url = new URL(strUrl);
// Creating an http connection to communicate with url
// urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
//urlConnection.connect();
InputStream inputStream = connection.getInputStream();
if (inputStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
inputStream.close();
//Log.i(TAG, "The result: " + result.toString());
//return this result
}
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
return json;
}
}