2012-04-27 9 views
0

私のアンドロイドアプリでMapViewを実行しましたが、ルートB/Wを2ポイント表示しましたが、コードは実装されていましたが、半分しか表示されませんでした。どのようにGoogleマップ上の完全なルートの2つのポイントを表示するには?

MapdirectionsActivity.java

public class MapdirectionsActivity extends MapActivity { 
    /** Called when the activity is first created. */ 

    MapView mapView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mapView = (MapView) findViewById(R.id.mapview); 
     double src_lat = 17.3667; // the source 
     double src_long = 78.4667; 
     double dest_lat = 18.9647; // the destination 
     double dest_long = 72.8258; 
     GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6)); 
     GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),(int) (dest_long * 1E6)); 
     DrawPath(srcGeoPoint, destGeoPoint, mapView); 
     mapView.getController().animateTo(srcGeoPoint); 
     mapView.getController().setZoom(7); 
    } 

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

    private void DrawPath(GeoPoint src, GeoPoint dest, MapView mMapView) { 

     // connect to map web service 
     StringBuilder urlString = new StringBuilder(); 
     urlString.append("http://maps.google.com/maps?f=d&hl=en"); 
     urlString.append("&saddr=");// from 
     urlString.append(Double.toString((double) src.getLatitudeE6()/1.0E6)); 
     urlString.append(","); 
     urlString.append(Double.toString((double) src.getLongitudeE6()/1.0E6)); 
     urlString.append("&daddr=");// to 
     urlString.append(Double.toString((double) dest.getLatitudeE6()/1.0E6)); 
     urlString.append(","); 
     urlString.append(Double.toString((double) dest.getLongitudeE6()/1.0E6)); 
     urlString.append("&ie=UTF8&om=1&output=kml"); 
     Log.d("Map directions", "URL= " + urlString.toString()); 

     // get the kml (XML) doc. And parse it to get the coordinates(direction route). 
     Document doc = null; 
     HttpURLConnection urlConnection = null; 
     URL url = null; 
     try { 
      url = new URL(urlString.toString()); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.connect(); 

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      doc = db.parse(urlConnection.getInputStream()); 

      if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) { 
       String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue(); 
       Log.d("Map directions", "path= " + path); 
       String[] pairs = path.split(" "); 
       String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude 
                 // lngLat[1]=latitude 
                 // lngLat[2]=height 
       GeoPoint startGP = new GeoPoint(
         (int) (Double.parseDouble(lngLat[1]) * 1E6), 
         (int) (Double.parseDouble(lngLat[0]) * 1E6)); 

       mMapView.getOverlays().add(new MyOverLay(startGP, startGP, 1));//starting point 
       GeoPoint gp1 = null; 
       GeoPoint gp2 = startGP; 
       for (int i = 1; i <pairs.length; i++) 
       { 
        lngLat = pairs[i].split(","); 
        gp1 = gp2; 
        // watch out! For GeoPoint, first:latitude, second:longitude 
        gp2 = new GeoPoint(
          (int) (Double.parseDouble(lngLat[1]) * 1E6), 
          (int) (Double.parseDouble(lngLat[0]) * 1E6)); 

        mMapView.getOverlays().add(new MyOverLay(gp1, gp2, 2));//route 
       } 

       mMapView.getOverlays().add(new MyOverLay(dest, dest, 3)); //last point 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
:私は画面が2つの赤い点を撃っているmistake.hereと、次のブルールートを http://www.flickr.com/photos/[email protected]/7117948377/
やっていることはコードです助けてください多くの方法で試してみましたが、それを解決できませんでした

MyOverLay.java

public class MyOverLay extends Overlay { 
    private GeoPoint gp1; 
    private GeoPoint gp2; 
    private int mRadius = 6; 
    private int mode = 0; 

    public MyOverLay(GeoPoint gp1, GeoPoint gp2, int mode) // Constructor 
    { 
     this.gp1 = gp1; 
     this.gp2 = gp2; 
     this.mode = mode; 
    } 

    public int getMode() { 
     return mode; 
    } 

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

     Projection projection = mapView.getProjection(); 
     if (shadow == false) { 
      Paint paint = new Paint(); 
      paint.setAntiAlias(true); 
      Point point = new Point(); 
      projection.toPixels(gp1, point); 
      // start 
      if (mode == 1) { 
       paint.setColor(Color.RED); 
       RectF oval = new RectF(point.x - mRadius, point.y - mRadius, 
         point.x + mRadius, point.y + mRadius); 

       canvas.drawOval(oval, paint);// start point 
      } 
      // mode=2&#65306;path 
      else if (mode == 2) { 
       paint.setColor(Color.BLUE); 
       Point point2 = new Point(); 
       projection.toPixels(gp2, point2); 
       paint.setStrokeWidth(5); 
       paint.setAlpha(120); 
       canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);// mode=2&#65306;path 
      } 
      /* mode=3&#65306;end */ 
      else if (mode == 3) { 
       /* the last path */ 

       paint.setColor(Color.RED); 
       Point point2 = new Point(); 
       projection.toPixels(gp2, point2); 
       paint.setStrokeWidth(5); 
       paint.setAlpha(120); 
       canvas.drawLine(point.x, point.y, point2.x, point2.y, paint); 
       RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius, 
         point2.x + mRadius, point2.y + mRadius); 

       paint.setAlpha(255); 
       canvas.drawOval(oval, paint);/* end point */ 
      } 
     } 
     return super.draw(canvas, mapView, shadow, when); 
    } 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:apiKey="0E2QBBM0fi1TSN6J0hCLbETO8oscApDt5CDqgbQ" 
    android:clickable="true" 
    android:enabled="true" /> 

答えて

1

このlinkを参照すると、アプリケーションに運転経路が表示されます。プロジェクトのリンクにある4つのクラスを追加し、ルートを表示する必要があるときは、次の行を呼び出すだけです。

SharedData data = SharedData.getInstance(); 
data.setAPIKEY("0RUTLH7cqd6yrZ0FdS0NfQMO3lioiCbnH-BpNQQ");//set your map key here 
data.setSrc_lat(17);//set your src lat 
data.setSrc_lng(78);//set your src lng 
data.setDest_lat(18);//set your dest lat 
data.setDest_lng(77);//set your dest lng 
startActivity(new Intent(YourActivity.this,RoutePath.class));//add RoutePath in your manifeast file 

//はまた、あなたのmanifeastファイルにそれが働いた

+0

おかげで権限を追加します。.. –

関連する問題