画两个点之间的线路在谷歌android系统中的地图 [英] Draw line between two points in google map in android

查看:159
本文介绍了画两个点之间的线路在谷歌android系统中的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  借鉴线路/路径谷歌地图

我怎么能画beetween两点线在谷歌地图?

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

推荐答案

公共类DrivingDirection扩展MapActivity工具         IDirectionsListener {

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() {
}

} 上述code得出两个点之间在地图上线的Andr​​oid。 我希望这是帮助。

} The above code draw line between two point on Map in android. I hope this is help.

这篇关于画两个点之间的线路在谷歌android系统中的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆