2011-12-07 9 views

Odpowiedz

10

Tak, otrzymujesz wartość czasu i dystansu, a także wiele szczegółów dotyczących kierunku jazdy, pieszych itp. wszystko co masz z usługi Google kierunek api

sprawdzić nasze ten łączy

http://code.google.com/apis/maps/documentation/directions/

+0

dla konkretnych informacji + 1 –

+0

dziękuję za odpowiedź ur., Ale to jest dla javascript potrzebuję do aplikacji na telefon android .. –

+1

możesz korzystać z tej usługi w Android również wystarczy zadzwonić do usługi za pomocą pakietu http – Pratik

10
Location location1 = new Location(""); 
    location1.setLatitude(lat); 
    location1.setLongitude(long); 

    Location location2 = new Location(""); 
    location2.setLatitude(lat); 
    location2.setLongitude(long); 

    float distanceInMeters = location1.distanceTo(location2); 

EDIT:

//For example spead is 10 meters per minute. 
    int speedIs10MetersPerMinute = 10; 
    float estimatedDriveTimeInMinutes = distanceInMeters/speedIs10MetersPerMinute; 

proszę zobaczyć, także jeśli powyżej nie pracuje dla ty:

Calculate distance between two points in google maps V3

+0

Myślę, że to zwróci km nie czas –

+0

To spowoduje powrót odległości w metrach i to dlatego napisałem "Teraz masz odległośćInmeters więc możesz obliczyć szacunkowy czas jazdy. " –

+0

czy możesz mi powiedzieć, jak mogę dostać czas? –

-2
Calculate Distance:- 
    float distance; 
      Location locationA=new Location("A"); 
      locationA.setLatitude(lat); 
      locationA.setLongitude(lng); 

      Location locationB = new Location("B"); 
      locationB.setLatitude(lat); 
      locationB.setLongitude(lng); 

      distance = locationA.distanceTo(locationB)/1000; 

      LatLng From = new LatLng(lat,lng); 
      LatLng To = new LatLng(lat,lng); 

      Calculate Time:- 
      int speedIs1KmMinute = 100; 
      float estimatedDriveTimeInMinutes = distance/speedIs1KmMinute; 
       Toast.makeText(this,String.valueOf(distance+ 
"Km"),Toast.LENGTH_SHORT).show(); 
      Toast.makeText(this,String.valueOf(estimatedDriveTimeInMinutes+" Time"),Toast.LENGTH_SHORT).show(); 
+0

To nie bierze pod uwagę dróg. Na przykład powiedziałbym, że mogę jechać z Kalifornii na Hawaje. – Feathercrown

0

Tak jak już jest described by Praktik, możesz użyć Google API wskazówek, aby oszacować czas potrzebny do dostać się z jednego miejsca do drugiego, biorąc pod uwagę kierunki i ruch. Ale nie musisz korzystać z interfejsu API sieci Web i budować własnego opakowania, zamiast tego użyj usługi Java implementation dostarczonej przez samą firmę Google, która jest dostępna za pośrednictwem repozytorium Maven/gradle.

  1. Add the google-maps-services to your app's build.gradle:

    dependencies { 
        compile 'com.google.maps:google-maps-services:0.2.5' 
    } 
    
  2. Wykonaj żądanie i wyodrębnić czas trwania:

    // - Put your api key (https://developers.google.com/maps/documentation/directions/get-api-key) here: 
    private static final String API_KEY = "AZ.." 
    
    /** 
    Use Google's directions api to calculate the estimated time needed to 
    drive from origin to destination by car. 
    
    @param origin The address/coordinates of the origin (see {@link DirectionsApiRequest#origin(String)} for more information on how to format the input) 
    @param destination The address/coordinates of the destination (see {@link DirectionsApiRequest#destination(String)} for more information on how to format the input) 
    
    @return The estimated time needed to travel human-friendly formatted 
    */ 
    public String getDurationForRoute(String origin, String destination) 
        // - We need a context to access the API 
        GeoApiContext geoApiContext = new GeoApiContext.Builder() 
         .apiKey(apiKey) 
         .build(); 
    
        // - Perform the actual request 
        DirectionsResult directionsResult = DirectionsApi.newRequest(geoApiContext) 
          .mode(TravelMode.DRIVING) 
          .origin(origin) 
          .destination(destination) 
          .await(); 
    
        // - Parse the result 
        DirectionsRoute route = directionsResult.routes[0]; 
        DirectionsLeg leg = route.legs[0]; 
        Duration duration = leg.duration; 
        return duration.humanReadable; 
    } 
    

Dla uproszczenia, ten kod nie obsługuje wyjątki, przypadki błędów (np brak znaleziona trasa -> routes.length == 0), nie przeszkadza też więcej niż jeden route lub leg. Pochodzenie i przeznaczenie mogą być również ustawić bezpośrednio jako LatLng przypadkach (patrz DirectionsApiRequest#origin(LatLng) i DirectionsApiRequest#destination(LatLng)

Dalsze czytanie:.? android.jlelse.eu - Google Maps Directions API

Powiązane problemy