在Android地图上绘制路线不起作用......?

时间:2012-03-11 09:59:34

标签: android google-maps

我一直在尝试使用这段代码一周。路线没有出现。我的代码如下。

我正在尝试在两个geopoints之间绘制一条路线 - 我从网络服务中检索的位置。

我的日志没有显示任何错误。

public class TesterGTC extends MapActivity {

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:2488/Service1.asmx";
    private static final String METHOD_NAME1 = "lastKnownLocationAllValues";
    private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME1;
    private List<Overlay> mapOverlays;
    private Projection projection;  
    MapView mapView;
    double latitude;
    double longitude;
    double endlat;
    double endlong;
    GeoPoint geoPoint;
    MapController myMC;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // Suppress title bar to give more space
        setContentView(R.layout.googletrackingclient);


        final String orderID = GoogleTrackingMenu.epcID;
        final String vehicleid = GoogleTrackingMenu.vehicleid;
        Thread t = new Thread(new Runnable() {
            public void run() {
                String u = orderID;
                String v = vehicleid;

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

                PropertyInfo propInfo = new PropertyInfo();
                propInfo.name = "OID";
                propInfo.type = PropertyInfo.STRING_CLASS;
                request.addProperty(propInfo, u);

                PropertyInfo propInfo2 = new PropertyInfo();
                propInfo2.name = "vehicleID";
                propInfo2.type = PropertyInfo.STRING_CLASS;
                request.addProperty(propInfo2, v);

                final TextView textview = (TextView) findViewById(R.id.id1);
                final TextView myLoc = (TextView) findViewById(R.id.id2);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;

                envelope.setOutputSoapObject(request);

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

                final ArrayList<EPCISGPSResult> resultList = new ArrayList<EPCISGPSResult>();
                try {

                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    final SoapObject resultRequestSOAP = (SoapObject) envelope
                            .getResponse();
                    final int resultInt = resultRequestSOAP.getPropertyCount();
                    for (int i = 0; i < resultInt; i++) {
                        SoapObject resultRequest = (SoapObject) resultRequestSOAP
                                .getProperty(i);
                        String vehicleID = resultRequest.getProperty("vehicleID").toString();
                        String driverName = resultRequest.getProperty("driverName").toString();
                        String latitude = resultRequest.getProperty("latitude").toString();
                        String longitude = resultRequest.getProperty("longitude").toString();
                        String startVenue = resultRequest.getProperty("startVenue").toString();
                        String destination = resultRequest.getProperty("destination").toString();
                        String dateReceived = resultRequest.getProperty("dateReceived").toString();
                        String utc = resultRequest.getProperty("utc").toString();
                        String orderID = resultRequest.getProperty("orderID").toString();
                        EPCISGPSResult e = new EPCISGPSResult(vehicleID,driverName, latitude, longitude, startVenue,destination, dateReceived, utc, orderID);
                        resultList.add(e);

                    }

                    latitude = Double.parseDouble(resultList.get(0).getLatitude());
                    longitude = Double.parseDouble(resultList.get(0).getLongitude());
                    endlat = Double.parseDouble(resultList.get(resultList.size()-1).getLatitude());
                    endlong = Double.parseDouble(resultList.get(resultList.size()-1).getLongitude());
                     int beglat = (int)latitude* 1000000;
                     int endinglat = (int)endlat* 1000000;
                     int beglong = (int)longitude* 1000000;
                     int endinglong = (int)endlong* 1000000;
                     final GeoPoint gP1 = new GeoPoint(beglat, beglong);
                     final GeoPoint gP2 = new GeoPoint(endinglat, endinglong);



        TesterGTC.this.runOnUiThread(new Runnable() {
            public void run() {
                int pointer = 0;
                pointer = 1;
                 TextView tview = (TextView) findViewById(R.id.id1);

                 tview.setText("before map");
                mapView = (MapView) findViewById(R.id.myGMap);
                mapView.setBuiltInZoomControls(true);
                mapView.setSatellite(true);

                myMC = mapView.getController();
                myMC.setZoom(15);
                int color = Color.RED;
                mapOverlays = mapView.getOverlays();        
                projection = mapView.getProjection();

                TextView txxview = (TextView) findViewById(R.id.id2);

                 txxview.setText("after map");
                MyOverlay newO = new MyOverlay(gP1, gP2, color);

               /* TextView textview = (TextView) findViewById(R.id.id1);

                textview.setText("This is happening");*/

                mapOverlays.add(newO);





            }       });

    } 
    catch (final Exception e) {
        TesterGTC.this.runOnUiThread(new Runnable() {
            public void run() {
                TextView textview = (TextView) findViewById(R.id.id1);

                textview.setText("Your error is: " + e.getMessage().toString());
            }
        });

    } finally {

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

    class MyOverlay extends Overlay{
        GeoPoint gp1;
        GeoPoint gp2;
        int color;

        public MyOverlay(GeoPoint gp1, GeoPoint gp2, int color){
            this.gp1 = gp1;
            this.gp2 = gp2;
            this.color = color;

        }   

        public void draw(Canvas canvas, MapView mapView, boolean shadow, GeoPoint gP1 , GeoPoint gP2){
            super.draw(canvas, mapView, shadow);


            TextView textview = (TextView) findViewById(R.id.id1);

            textview.setText("This is ALSO happening");

            Paint   mPaint = new Paint();
            mPaint.setDither(true);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(2);

            Point p1 = new Point();
            Point p2 = new Point();
            Path path = new Path();

            projection.toPixels(gP1, p1);
            projection.toPixels(gP2, p2);

            path.moveTo(p2.x, p2.y);
            path.lineTo(p1.x,p1.y);

            canvas.drawPath(path, mPaint);
        }

    }
}

2 个答案:

答案 0 :(得分:0)

这里saddr = source&amp; daddr =目的地位置。

public void showDirections(View view) {
        final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ latitude + "," + longitude + "&daddr=" + latitude + "," + longitude));
        intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
        startActivity(intent);
    }

答案 1 :(得分:0)

public class HelloItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext = getApplicationContext();
    Bitmap bmp = BitmapFactory.decodeFile("pushpin.png");
    Drawable drawable = new BitmapDrawable(bmp);

    public HelloItemizedOverlay(Drawable drawable) {

        super(boundCenterBottom(drawable));
        populate();

    }

    public HelloItemizedOverlay(Drawable drawable, Context context) {
        super(boundCenterBottom(drawable));
        mContext = getApplicationContext();
        populate();
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(
                GoogleTrackingPage.this);
        // add the overlay item's title and snippet or create owner

        String newMessage = "Your order has the following location details \n\nVehicleID: "
                + vehicle
                + "\nDriver: "
                + driver
                + "\nStart Venue: "
                + starting
                + "\nFinal Destination: "
                + desti
                + "\nLast Updated on:" + dateR;
        dialog.setTitle("Order Details for " + orderidfrommenu + " at "
                + locationString);
        dialog.setMessage(newMessage);
        dialog.show();

        return true;
    }
    @Override
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) 
     {
         Paint paint = null;
         int x = myList.size();
         int lastPoint = x-1;

         if(myList.size() <= 2){
             latitude = Double.parseDouble(myList.get(0).getLatitude());
                longitude = Double.parseDouble(myList.get(0).getLongitude());
         }
         else{
             int lP = myList.size()-2;
             latitude = Double.parseDouble(myList.get(lP).getLatitude());
             longitude = Double.parseDouble(myList.get(lP).getLongitude());
         }

            endlat = Double.parseDouble(myList.get(myList.size() - 1).getLatitude());
            endlong = Double.parseDouble(myList.get(myList.size() - 1).getLongitude());


            int beglat = (int)latitude* 1000000;
             int endinglat = (int)endlat* 1000000;
             int beglong = (int)longitude* 1000000;
             int endinglong = (int)endlong* 1000000;
             GeoPoint gP1 = new GeoPoint(beglat, beglong);
             GeoPoint gP2 = geoPoint;


         gP1=mOverlays.get(0).getPoint(); 


         Projection projection = mapView.getProjection();
            if (shadow == false) 
            {
                 paint = new Paint();
                paint.setAntiAlias(true);


                Point point = new Point();
                projection.toPixels(gP1, point);
                paint.setColor(Color.RED);
                Point point2 = new Point();
                projection.toPixels(gP2, point2);
                paint.setStrokeWidth(5);
                canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                        (float) point2.y, paint);



            }

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

}