0

Possible Duplicate:
Drawing a line/path on Google Maps

How can i draw line beetween two points in google map?

Community
  • 1
  • 1
Manisha
  • 217
  • 2
  • 4
  • 9
  • 1
    please check the related questions on the site first ... contains a lot of answers to your question – ddewaele Jun 01 '11 at 10:51
  • first of all,You should receive the co-ordinates between source and end co-ordinates then you can draw the line... [Check this](http://stackoverflow.com/questions/2023669/j2me-android-blackberry-driving-directions-route-between-two-locations/2023685#2023685) – Kakey May 30 '11 at 11:38
  • I've merged your two accounts together and the two duplicate questions you asked. Please don't create multiple accounts, and please don't ask the same question over and over. Both are considered abuses of the system. –  Jun 01 '11 at 13:29
  • Hey Will my account was suspended and when regained all my reputations were lost.. Can you help with that.. IT was the effort of my past 2 years :( – Suresh Varma Aug 25 '11 at 07:19

1 Answers1

0
public class DrivingDirection extends MapActivity implements
        IDirectionsListener {

    Button btnRedirectHome;
    Vector<GeoPoint> mapData = new Vector<GeoPoint>();
    Vector<GeoPoint> NearData = new Vector<GeoPoint>();
    MapView mapView;
    MapController mc;
    GeoPoint p;
    GeoPoint endpoint;
    MapOverlay mapOverlay;
    private AlertDialog.Builder alertDialog1, alertDialog;

    boolean exception = false;

    // ** Called when the activity is first created. *//*
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.courtdetaildetdirection_layout);

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mc = mapView.getController();

        Bundle extras = getIntent().getExtras();
        String courtId = extras != null ? extras.getString("CourtId") : "";
        
        
        if(GUIStatics.previousActivityNameFalg.equalsIgnoreCase("MatchDetailActivity"))
        {
            if (GUIStatiMethods
                    .connectionCheck(DrivingDirection.this)) {
                Court_Player_LocationFatcher objectCourt_Player_LocationFatch = new Court_Player_LocationFatcher();
                objectCourt_Player_LocationFatch.Court_Player_LocationFatch(GUIStatics.strUserID,courtId);
                final CourtDetailFatcher myCourtDetailFatcher = new CourtDetailFatcher();
                myCourtDetailFatcher.CourtDetailFatch(courtId);
            }
        }
        else if(GUIStatics.previousActivityNameFalg.equalsIgnoreCase("FavoritesActivity"))
        {
            if (GUIStatiMethods
                    .connectionCheck(DrivingDirection.this)) {
                Court_Player_LocationFatcher objectCourt_Player_LocationFatch = new Court_Player_LocationFatcher();
                objectCourt_Player_LocationFatch.Court_Player_LocationFatch(GUIStatics.strUserID,courtId);
                final CourtDetailFatcher myCourtDetailFatcher = new CourtDetailFatcher();
                myCourtDetailFatcher.CourtDetailFatch(courtId);
            }
        }
        else
        {
            if (GUIStatiMethods
                    .connectionCheck(DrivingDirection.this)) {
                Court_Player_LocationFatcher objectCourt_Player_LocationFatch = new Court_Player_LocationFatcher();
                objectCourt_Player_LocationFatch.Court_Player_LocationFatch(GUIStatics.strUserID,courtId);

                final CourtDetailFatcher myCourtDetailFatcher = new CourtDetailFatcher();
                myCourtDetailFatcher.CourtDetailFatch(courtId);
            }
        }

        TextView TextViewCourtDetailHeader = (TextView) this
                .findViewById(R.id.TextViewCourtDetailHeader);
        TextView TextViewCourtDetailAddress = (TextView) this
                .findViewById(R.id.TextViewCourtDetailAddress);
        TextView TextViewCourtDetailNoOfCourtAndType = (TextView) this
                .findViewById(R.id.TextViewCourtDetailNoOfCourtAndType);
        TextView TextViewCourtDetailCourtType = (TextView) this
                .findViewById(R.id.TextViewCourtDetailCourtType);
        TextView TextViewCourtDetailLight = (TextView) this
                .findViewById(R.id.TextViewCourtDetailLight);

        TextViewCourtDetailHeader
                .setText(CourtDetailDataHandler.object.COURTNAME);
        TextViewCourtDetailAddress
                .setText(CourtDetailDataHandler.object.COURTADDRESS);
        TextViewCourtDetailNoOfCourtAndType
                .setText(CourtDetailDataHandler.object.NUMBEROFCOURT);
        TextViewCourtDetailCourtType
                .setText(CourtDetailDataHandler.object.COURTTYPE);
        TextViewCourtDetailLight.setText("Lights: "
                + CourtDetailDataHandler.object.COURTLIGHT);
        String url = "http://maps.google.com/maps?f=d&hl=en&saddr="
                + Court_Player_LocationHandler.mCOURTLAT + ","
                + Court_Player_LocationHandler.mCOURTLON + "&daddr="
                + Court_Player_LocationHandler.mUSERLAT + ","
                + Court_Player_LocationHandler.mUSERLON
                + "&ie=UTF8&0&om=0&z=20&output=kml";
        Document d = GetDataFromServer(url);

        if (exception) {
            exception = false;
            GeoPoint source = new GeoPoint(
                    (int) (Double
                            .parseDouble(Court_Player_LocationHandler.mCOURTLAT) * 1E6),
                    (int) (Double
                            .parseDouble(Court_Player_LocationHandler.mCOURTLON) * 1E6));
            mapData.add(source);

            GeoPoint destination = new GeoPoint(
                    (int) (Double
                            .parseDouble(Court_Player_LocationHandler.mUSERLAT) * 1E6),
                    (int) (Double
                            .parseDouble(Court_Player_LocationHandler.mUSERLON) * 1E6));
            mapData.add(destination);

        } else {
            ParseServerData(d);
        }

        LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
        View zoomView = mapView.getZoomControls();

        zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);

        mc = mapView.getController();

        for (int i = 0; i < mapData.size(); i++) {
            GeoPoint gp = mapData.get(i);
            mc.animateTo(gp);
        }

        for (int i = 0; i < NearData.size(); i++) {
            GeoPoint gp = mapData.get(i);
            mc.animateTo(gp);
        }

        mc.setZoom(18);

        // ---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();

    }

    class MapOverlay extends com.google.android.maps.Overlay {
        Paint innerPaint;
        int infoWindowOffsetX, infoWindowOffsetY, testX, testY, id;
        boolean showWindow = false;
        Bitmap bitmap, bmp, bitMoreInformation;
        String argName;
        boolean temp = true;

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);

            bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.marker);
            // ---translate the GeoPoint to screen pixels---
            Paint myPaintBlack1 = new Paint();
            myPaintBlack1.setColor(Color.BLUE);
            myPaintBlack1.setStyle(Style.STROKE);
            myPaintBlack1.setStrokeWidth(4f);

            Point screenPts[] = new Point[mapData.size()];

            for (int i = 0; i < mapData.size(); i++) {
                Point screenPt = new Point();
                GeoPoint gp = mapData.get(i);
                mapView.getProjection().toPixels(gp, screenPt);// screenPts[i]);
                screenPts[i] = screenPt;
                if (i == 0)
                    canvas.drawBitmap(bmp, screenPts[i].x - bmp.getWidth() / 2,
                            screenPts[i].y - bmp.getHeight(), null);

                if (i == mapData.size() - 1)
                    canvas.drawBitmap(bmp, screenPts[i].x - bmp.getWidth() / 2,
                            screenPts[i].y - bmp.getHeight(), null);

            }

            for (int i = 0; i < NearData.size(); i++) {
                Point screenPs = new Point();
                GeoPoint gp = mapData.get(i);
                mapView.getProjection().toPixels(gp, screenPs);// screenPts[i]);
                canvas.drawBitmap(bmp, screenPs.x - bmp.getWidth() / 2,
                        screenPs.y - bmp.getHeight(), null);
            }

            for (int i = 1; i < screenPts.length; i++) {
                canvas.drawLine(screenPts[i - 1].x, screenPts[i - 1].y,
                        screenPts[i].x, screenPts[i].y, myPaintBlack1);
            }

            return true;
        }

        public Paint getInnerPaint() {
            if (innerPaint == null) {
                innerPaint = new Paint();
                innerPaint.setARGB(225, 75, 75, 75); // gray
                innerPaint.setAntiAlias(true);
            }
            return innerPaint;
        }

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {

            return true;
        }

    }

    public Document GetDataFromServer(String url) {
        try {
            URL updateURL = new URL(url);
            URLConnection conn = updateURL.openConnection();
            InputStream is = conn.getInputStream();
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(is);
            is.close();
            return document;

        } catch (Exception e) {
            exception = true;
            System.out.print("Exception:-" + e);
        }
        exception = true;
        return null;
    }

    private void ParseServerData(Document doc) {

        Element rootElement = doc.getDocumentElement();
        rootElement.normalize();

        NodeList nodeLst = doc.getElementsByTagName("LookAt");
        int c = nodeLst.getLength();
        System.out.println("count=  " + c);
        for (int s = 0; s < nodeLst.getLength(); s++) {
            Node fstNode = nodeLst.item(s);
            if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                // Points p = new Points();
                Element Elmnt = (Element) fstNode;
                double lat = Double
                        .parseDouble(getTextValue(Elmnt, "latitude"));
                double lng = Double
                        .parseDouble(getTextValue(Elmnt, "longitude"));
                GeoPoint gp = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
                mapData.add(gp);
            }
        }
    }

    public String getTextValue(Element ele, String tagName) {
        String textVal = "";

        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            NodeList ndlist = el.getChildNodes();
            Node node = ndlist.item(0);
            if (node != null)
                textVal = node.getNodeValue();
        }
        return textVal;
    }

    // handler for the background updating
    Handler progressHandler = new Handler() {
        public void handleMessage(Message msg) {

        }
    };

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public void onDirectionsAvailable(Route route,
            com.cipl.Courts.DrivingDirections.Mode mode) {
    }

    public void onDirectionsNotAvailable() {
    }
}

The above code draw line between two point on Map in android.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
DynamicMind
  • 4,240
  • 1
  • 26
  • 43