当用户拖动图钉时,我试图在地图上动态绘制路线。我可以在两个地理点之间绘制路线,但不能在它们之间绘制道路路线。
我正在使用此代码在正在使用的地图上放置图钉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&gp2) 添加到叠加层中,以通过覆盖draw()
方法在地图上绘制。一旦我得到这些地理点,我就可以绘制路线。但无法发送地理点。卡在这里。