2012-03-11 75 views
0

我一直在努力工作这段代码一个星期了。路线没有出现。我的代码如下。在Android地图上绘制路线不工作...?

我想绘制两个geopoints之间的路线 - 我从Web服务检索的位置。

我的日志不显示任何错误。

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); 
     } 

    } 
} 
+0

你想使用谷歌地图?我开发了一个应用程序,并使用Google地图的Web服务,在该服务中,我传递了源地址和目标地址,并将返回给您整个地图。 – Scorpion 2012-03-12 11:23:23

回答

0

这里SADDR =源& 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

好吧,我会尝试这个代码,并回复给你 - 非常感谢您的回应:) – Karishma 2012-03-12 15:01:34

+0

嗨 - 你的解决方案适合!只有最后一个问题 - 当它转到Google地图时 - 它会显示路线,然后加载驾驶路线页面。我如何确保 - 行车路线页面不显示? – Karishma 2012-03-17 05:17:11

+1

嗨 - 我还有最后一个问题。您的解决方案可以正常工作,但每次从项目数据库中检索新位置时,我都需要更新地图。从某种意义上说,我必须展示在地图上移动的物体 - 实时跟踪。你有没有做过类似的事情?你介意分享吗? – Karishma 2012-03-21 04:29:17

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); 
    } 

} 
+0

请至少编写一段代码的小解释。 – 2012-10-26 11:42:40