想在android中的任意两个位置之间画一条线

时间:2012-08-13 10:47:49

标签: android google-maps overlay android-mapview mapactivity

这是我的main活动

import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class DrawActivity extends MapActivity {
 MapView myMapView = null;
 MapController myMC = null;
 GeoPoint geoPoint = null;
  String pairs[]=null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {


  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  myMapView = (MapView) findViewById(R.id.mapview);
  geoPoint = null;
  myMapView.setSatellite(false);
  System.out.println("before pairs");
  String pairs[] = getDirectionData("Hyderabad","hyderguda");

  System.out.println("pairs:"+pairs[1]);
  String[] lngLat = pairs[0].split(",");

  // STARTING POINT
  GeoPoint startGP = new GeoPoint(
    (int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
      .parseDouble(lngLat[0]) * 1E6));

  myMC = myMapView.getController();
  geoPoint = startGP;
  myMC.setCenter(geoPoint);
  myMC.setZoom(15);
  myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

  // NAVIGATE THE PATH

  GeoPoint gp1;
  GeoPoint gp2 = startGP;

  for (int i = 1; i < pairs.length; i++) {
   lngLat = pairs[i].split(",");
   gp1 = gp2;
   // watch out! For GeoPoint, first:latitude, second:longitude

   gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
     (int) (Double.parseDouble(lngLat[0]) * 1E6));
   myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
   Log.d("xxx", "pair:" + pairs[i]);
  }

  // END POINT
  myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));

  myMapView.getController().animateTo(startGP);
  myMapView.setBuiltInZoomControls(true);
  myMapView.displayZoomControls(true);

 }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }

 private String[] getDirectionData(String srcPlace, String destPlace) {




  String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
   + srcPlace + "&daddr=" + destPlace
   + "&ie=UTF8&0&om=0&output=kml";

  Log.d("URL", urlString);
  Document doc = null;
  HttpURLConnection urlConnection = null;
  URL url = null;
  String pathConent = "";

  try {

   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();
   doc = db.parse(urlConnection.getInputStream());

 //The above code "doc" is getting null value so thats why its getting crashed here itself not going further execution and returning as `nullpointerexception`
  } catch (Exception e) {
  }

  NodeList nl = doc.getElementsByTagName("LineString");
  for (int s = 0; s < nl.getLength(); s++) {
   Node rootNode = nl.item(s);
   NodeList configItems = rootNode.getChildNodes();
   for (int x = 0; x < configItems.getLength(); x++) {
    Node lineStringNode = configItems.item(x);
    NodeList path = lineStringNode.getChildNodes();
    pathConent = path.item(0).getNodeValue();
   }
  }
  String[] tempContent = pathConent.split(" ");
  return tempContent;
 }

}

和我的另一部分代码     包com.hands;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class DirectionPathOverlay extends Overlay {

private GeoPoint gp1;
private GeoPoint gp2;

public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
    this.gp1 = gp1;
    this.gp2 = gp2;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
        long when) {
    // TODO Auto-generated method stub
    Projection projection = mapView.getProjection();
    if (shadow == false) {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(gp1, point);
        paint.setColor(Color.BLUE);
        Point point2 = new Point();
        projection.toPixels(gp2, point2);
        paint.setStrokeWidth(2);
        canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                (float) point2.y, paint);
    }
    return super.draw(canvas, mapView, shadow, when);
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    // TODO Auto-generated method stub

    super.draw(canvas, mapView, shadow);
}

}

以上是我的完整源代码plz帮助我解决这个问题

1 个答案:

答案 0 :(得分:4)

您使用的是output=kml,此参数不再有效。您收到错误,因为此URL现在检索网页而不是KML文件。

自2012年7月27日以来,这种通过解析KML文件从Google中提取Google方向的方式已不再可用(因为Google已经更改了检索Google方向的结构,现在您只能通过JSON或XML获取它),它是时候将代码迁移到JSON而不是KML。

在我自己的问题here中查看答案。