在android中的谷歌地图中的两点之间画线

时间:2011-05-30 11:33:25

标签: android google-maps

  

可能重复:
  Drawing a line/path on Google Maps

如何在谷歌地图中的两点之间画线?

2 个答案:

答案 0 :(得分:0)

请参阅此问题的已接受答案:Drawing a line/path on Google Maps

答案 1 :(得分: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() {
    }
}

上面的代码在android中的Map上的两点之间画线。 我希望这是有帮助的。

相关问题