2012-05-01 16 views
0
String uri = "http://maps.google.com/maps?saddr=" + src_lat+","+src_lon+"&daddr="+des_lat1+","+des_lon1; 
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)); 
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
    startActivity(intent); 

ここでは、発信元と目的地の緯度と経度の両方がわかっている2つの場所間のルートを描画するコードを示します。しかしここで私の位置の経度と緯度をsrc_lonsrc_latに置き換えたいと思っています。その値はわかりません。この経度と緯度は私の現在の位置で変わります。このようなルートを描くことは可能ですか?目的地点にポイントを取得して経路を描く

+0

私が思うに、このリンクはあなたに役立つだろう http://stackoverflow.com/questions/4408671/android-draw-route-on-a-mapview-with-twoo-poi-s –

答えて

1

はこれを試してみてください。

1
private void DrawPath(GeoPoint src, GeoPoint dest, int color, 
         MapView mMapView01) { 

    // Connect to the 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&0&om=0&output=kml"); 
    Log.d("xxx","URL="+urlString.toString()); 

    //System.out.println(urlString); 
    // Get the KML (XML) document. 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().getNodeName(); 
      String path = doc.getElementsByTagName(
       "GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue(); 
      Log.d("xxx","path="+ path); 
      String [] pairs = path.split(" "); 
      String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height 
      // Source 
      GeoPoint startGP = new GeoPoint((int)(
       Double.parseDouble(lngLat[1])*1E6), 
       (int)(Double.parseDouble(lngLat[0])*1E6)); 
      //mMapView01.getOverlays().add(overlayitem); 
      GeoPoint gp1; 
      GeoPoint gp2 = startGP; 
      for(int i=1; i<pairs.length; i++) // The last one would crash. 
      { 
       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)); 
       mMapView01.getOverlays().add(new MapRouteOverlay(gp1,gp2,2,color)); 
       Log.d("xxx","pair:" + pairs[i]); 
      } 
      //mMapView01.getOverlays().add(new MapRouteOverlay(dest,dest, 3)); // Use the default color 
     } 
    } 
    catch (MalformedURLException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (ParserConfigurationException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (SAXException e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

ます。http: //stackoverflow.com/questions/4408671/android-draw-route-on-a-mapview-with-twoo-poi-s –

0

はい、現在地の緯度と経度を取得するには、Location Managerを使用します。 Location Strategiesを参照してください。また、The Google Directions APIとしてください。これは私が知っているソリューションが最適です

Intent i = new Intent(android.content.Intent.ACTION_VIEW, 
     Uri.parse("google.navigation:q=" +destLat+ ","+destLon+"")); 
this.startActivity(i); 

+0

GeoPoint myPoint = myLocationOverlay.getMyLocation(); int myLat = myPoint.getLatitudeE6(); int myLng = myPoint.getLongitudeE6(); 私はthis.butを試しましたが、うまくいきません。追加する必要があるものは何ですか?:( –

+0

Location Managerを使用しましたか?http://developer.android.com/guide/topics/ location/getting-user-location.html ..チュートリアルが確実に得られるでしょう... –

+0

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER; locationManager.requestLocationUpdates(locationProvider、0、0、locationListener);私はこれを使用します –

関連する問題