2012-06-22 2 views
0

В настоящее время я делаю приложение для Android, требующее получения проезда. У меня есть как источник, так и пункт назначения, широта и долгота, и я получил путь между двумя точками, но я не могу получить маршруты проезда. плз помогите мне ... код для пути приведен нижеКод Android для получения направления движения

  GeoPoint gp1; 
     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)); 
      myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2)); 
      Log.d("xxx", "pair:" + pairs[i]); 
     } 

     myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2)); 

     myMapView.getController().animateTo(startGP); 
     myMapView.setBuiltInZoomControls(true); 
     myMapView.displayZoomControls(true); 



} 


private String[] getDirectionData(String srcPlace, String destPlace) { 

     String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr=" 
       + srcPlace + "&daddr=" + destPlace 
       + "&ie=UTF8&0&om=0&output=kml"; 
     Log.d("URL", urlString); 
     Document doc = null; 
     HttpURLConnection urlConnection = null; 
     URL url = null; 
     String pathConent = ""; 
     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()); 

     } catch (Exception e) { 
     } 

     NodeList nl = doc.getElementsByTagName("LineString"); 
     for (int s = 0; s < nl.getLength(); s++) { 
      Node rootNode = nl.item(s); 
      NodeList configItems = rootNode.getChildNodes(); 
      for (int x = 0; x < configItems.getLength(); x++) { 
       Node lineStringNode = configItems.item(x); 
       NodeList path = lineStringNode.getChildNodes(); 
       pathConent = path.item(0).getNodeValue(); 
      } 
     } 
     String[] tempContent = pathConent.split(" "); 
     return tempContent; 
    } 

DirectionPathOverlay.java

 public class DirectionPathOverlay extends Overlay { 
private GeoPoint gp1; 
private GeoPoint gp2; 
public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) { 
    this.gp1 = gp1; 
    this.gp2 = gp2; 
} 

@Override 
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
     long when) { 
    // TODO Auto-generated method stub 
    Projection projection = mapView.getProjection(); 
    if (shadow == false) { 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     Point point = new Point(); 
     projection.toPixels(gp1, point); 
     paint.setColor(Color.BLUE); 
     Point point2 = new Point(); 
     projection.toPixels(gp2, point2); 
     paint.setStrokeWidth(2); 
     canvas.drawLine((float) point.x, (float) point.y, (float) point2.x, 
       (float) point2.y, paint); 
    } 
    return super.draw(canvas, mapView, shadow, when); 
} 

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    // TODO Auto-generated method stub 

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

} 
+0

проверить эти SO сообщения http://stackoverflow.com/questions/2643993/how-to-display-a-route-between-two-geocoords-in-google-maps и HTTP: // StackOverflow. com/questions/2023669/j2me-android-blackberry-driving-direction-route-between-two-locations/2023685 # 2023685 – Orlymee

+0

[http://stackoverflow.com/questions/1612533/android-drivingdirections-removed-since-api -1-0-как-To-Do-он-в-1-5-1-6] [1] [1]: http://stackoverflow.com/questions/1612533/android- ОКОНХпроверить это – Manikandan

+0

Я не хочу рисовать путь. я хочу получить проезд – user1057197

ответ

1

Попробуйте этот код, чтобы показать направление к месту от вашего текущего местоположения.

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     lat = (double) (location.getLatitude()); 
     lng = (double) (location.getLongitude()); 

    } else { 
     Log.d(TAG, "Cannot get the location"); 
    } 

    final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ lat + "," + lng + "&daddr="+hotelLat+","+hotelLng)); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
Смежные вопросы