当用户拖动引脚时,我试图动态地在地图上绘制路线。我可以在两个地理位置之间绘制路线,但不能在它们之间绘制道路路线。
使用this代码在地图上放置使用ItemizedOverlay
的图钉。我使用以下代码片段来获取kml文件,并从源头到目的地获取连续的地理位置。
public static String getUrl(double fromLat, double fromLon, double toLat,
double toLon) {// connect to map web service
System.out.println("getting URL");
StringBuffer urlString = new StringBuffer();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(Double.toString(fromLat));
urlString.append(",");
urlString.append(Double.toString(fromLon));
urlString.append("&daddr=");// to
urlString.append(Double.toString(toLat));
urlString.append(",");
urlString.append(Double.toString(toLon));
urlString.append("&ie=UTF8&0&om=0&output=kml");
return urlString.toString();
}
和
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(urlConnection.getInputStream());
if (document.getElementsByTagName("GeometryCollection").getLength() > 0) {
String path = document.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild().getFirstChild()
.getNodeValue();
if (path != null && path.trim().length() > 0) {
String[] pairs = path.trim().split(" ");
String[] lngLat = pairs[0].split(",");
if (lngLat.length < 3)
lngLat = pairs[1].split(",");
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++)
{
lngLat = pairs[i].split(",");
gp1 = gp2;
gp2 = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
map.invalidate();
final float[] results = new float[3];
Location.distanceBetween(gp1.getLatitudeE6()/1E6, gp1.getLongitudeE6()/1E6, gp2.getLatitudeE6()/1E6, gp2.getLongitudeE6()/1E6, results);
distance += results[0];
在这里,我需要将这些地理位置(gp1&amp; gp2)添加到叠加层,以便通过覆盖draw()
方法在地图上绘制。一旦我得到那些地理位置,我就可以绘制路线了。但是无法发送地理位置。被困在这里