ある場所から別の場所への推定運転時間を見つける必要があります。両方の場所の緯度と経度がありますが、その方法がわかりません。そのためのAPIはありますか?助けてくれてありがとう。
5 に答える
はい、時間と距離の値だけでなく、運転、歩行などのモードでの方向の詳細も表示されます。あなたがグーグルディレクションAPIサービスから得たすべて
このリンクを確認してください
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;
上記がうまくいかない場合は、こちらもご覧ください。
非推奨の注意事項以下で説明するソリューションは、APIキーが失われる可能性があるためAndroidアプリでの使用を目的としていないGoogleマップサービス用のGoogleのJavaクライアントに基づいています(コメントのPK Guptaに記載されています)。したがって、本番環境での使用はお勧めしません。
Praktikがすでに説明したように、Googleのルート案内APIを使用して、ルート案内と交通状況を考慮して、ある場所から別の場所に移動するのに必要な時間を見積もることができます。ただし、Web APIを使用して独自のラッパーを作成する必要はありません。代わりに、 Maven/gradleリポジトリから入手できるGoogle自体が提供するJava実装を使用してください。
google-maps-servicesをアプリのbuild.gradleに追加します。
dependencies { compile 'com.google.maps:google-maps-services:0.2.5' }
リクエストを実行し、期間を抽出します。
// - 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; }
簡単にするために、このコードは例外やエラーの場合(たとえば、ルートが見つからない-> routers.length == 0)を処理せず、複数のルートまたはレッグを気にすることもありません。LatLng
起点と終点をインスタンスとして直接設定することもできます(DirectionsApiRequest#origin(LatLng)
およびを参照してくださいDirectionsApiRequest#destination(LatLng)
。
http://maps.google.com/maps?saddr= {start_address}&daddr={destination_address}を使用することもできます
2つの場所の間の距離と時間とともに方向の詳細が表示されます
http://maps.google.com/maps?saddr=79.7189,72.3414&daddr=66.45,74.6333&ie=UTF8&0&om=0&output=kml
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();