Dan*_*lan 8 android routes highlight directions google-maps-android-api-2
是的,所以我目前正在我的应用中使用Google Directions API来检索两个位置之间的路线.
当我发送路线指示请求时,我会在JSON中检索有关路线的一些细节,包括沿路线的每条道路的名称,相应的开始和结束纬度坐标以及它们的折线值.
例如:如果我在两条道路之间发送请求http://maps.googleapis.com/maps/api/directions/json?origin=redfern+ave,+dublin&destination=limetree+ave,+dublin&sensor=false,我会得到遵循JSON响应(沿路线输出一条道路).
{
"distance" : {
"text" : "0.2 km",
"value" : 203
},
"duration" : {
"text" : "1 min",
"value" : 18
},
"end_location" : {
"lat" : 53.435250,
"lng" : -6.132140000000001
},
"html_instructions" : "Head \u003cb\u003eeast\u003c/b\u003e on \u003cb\u003eRedfern Ave.\u003c/b\u003e toward \u003cb\u003eMartello Court\u003c/b\u003e",
**"polyline" : {
"points" : "woceIvgmd@O}DOkDQqF"**
},
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的应用程序解析此信息,并在列表视图中列出道路和方向,如下所示:

我想要做的是在地图上突出显示从A到B的整个路线,但是我在网上找不到有关如何在新的Google Maps API v2上执行此操作的任何有用信息.我看到使用折线而不是叠加来在Google Maps v2上绘制线条,但是据我所知,它们只绘制直线,这对我来说毫无用处.无论如何使用我掌握的信息突出显示路线(道路名称,开始和结束纬度坐标,折线点?任何帮助表示赞赏.
此外,我看到响应中有一个"折线"值可能很有用,但我无法弄清楚如何解析或使用这些信息.有谁知道我如何能够理解这个值来绘制折线?
**"polyline" : {
"points" : "woceIvgmd@O}DOkDQqF"**
Run Code Online (Sandbox Code Playgroud)
编辑:我的解决方案代码在下面的答案中提供.
Dan*_*lan 36
经过大量的反复试验,我终于设法让它工作了!它现在完全突出显示地图上从A到B的指定路线(如下面的屏幕截图所示).对于今后可能需要它的人,我也提到了我的代码.

public class PolyMap extends Activity {
ProgressDialog pDialog;
GoogleMap map;
List<LatLng> polyz;
JSONArray array;
static final LatLng DUBLIN = new LatLng(53.344103999999990000,
-6.267493699999932000);
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(DUBLIN, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
new GetDirection().execute();
}
class GetDirection extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PolyMap.this);
pDialog.setMessage("Loading route. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
Intent i = getIntent();
String startLocation = i.getStringExtra("startLoc");
String endLocation = i.getStringExtra("endLoc");
startLocation = startLocation.replace(" ", "+");
endLocation = endLocation.replace(" ", "+");;
String stringUrl = "http://maps.googleapis.com/maps/api/directions/json?origin=" + startLocation + ",+dublin&destination=" + endLocation + ",+dublin&sensor=false";
StringBuilder response = new StringBuilder();
try {
URL url = new URL(stringUrl);
HttpURLConnection httpconn = (HttpURLConnection) url
.openConnection();
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader input = new BufferedReader(
new InputStreamReader(httpconn.getInputStream()),
8192);
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
String jsonOutput = response.toString();
JSONObject jsonObject = new JSONObject(jsonOutput);
// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);
JSONObject poly = route.getJSONObject("overview_polyline");
String polyline = poly.getString("points");
polyz = decodePoly(polyline);
} catch (Exception e) {
}
return null;
}
protected void onPostExecute(String file_url) {
for (int i = 0; i < polyz.size() - 1; i++) {
LatLng src = polyz.get(i);
LatLng dest = polyz.get(i + 1);
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true));
}
pDialog.dismiss();
}
}
/* Method to decode polyline points */
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19316 次 |
| 最近记录: |