2011-12-07 54 views

回答

10

是的,你可以得到時間和距離值以及許多像駕駛,步行等模式中的方向細節。你從谷歌的方向API服務得到了

檢查我們的這個鏈接

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

+0

對於確切信息+ 1 –

+0

感謝您的答覆。但那是爲JavaScript我需要爲Android手機應用程序.. –

+1

您可以使用此服務在Android也只是你需要打電話服務使用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); 

編輯

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

也請看到這一點,如果上面沒有作品你:

Calculate distance between two points in google maps V3

+0

我認爲這將返回公里沒有時間 –

+0

這將返回表和多數民衆贊成距離爲什麼我寫「現在你有distanceInMeters所以你可以計算出預計的駕駛時間。「 –

+0

你能告訴我如何獲得時間嗎? –

-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

這不考慮道路。例如,它會告訴我我可以從加州驅車前往夏威夷。 – Feathercrown

0

作爲已經described by Praktik,您可以使用Google的路線API來估計所需的時間從一個地方到另一個地方考慮​​路線和交通。但是,您不必使用Web API並構建自己的包裝器,而是使用Google自己提供的Java implementation,它可以通過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. 執行請求和提取的持續時間:

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

爲簡單起見,此代碼不處理異常,錯誤的情況下(例如,沒有路線發現 - > routes.length == 0),也不會打擾多個routeleg。出發地和目的地也可以直接設置爲LatLng實例(見DirectionsApiRequest#origin(LatLng)DirectionsApiRequest#destination(LatLng)

延伸閱讀:?android.jlelse.eu - Google Maps Directions API

相關問題