Google Direction Api
1.6
これは、デバイスの場所から目的地の場所など、2 つのポイント (場所) 間の方向パスを提供する Android ライブラリです。このライブラリを使用するには、Goolge Cloud Console でプロジェクトを作成し、Google Direction API とともに Map SDK を有効にする必要があります。セットアップがすべて完了したら、次の簡単な手順に従ってプロジェクトにコードを実装するだけです。
クリックしてビデオデモをご覧ください または YouTube の実装チュートリアル | |
画像 | ビデオデモ |
build.gradle
(プロジェクトレベル) ファイルに jitpack リポジトリを追加します。
allprojects {
repositories {
.. .
maven { url ' https://jitpack.io ' }
}
}
settings.gradle
で
dependencyResolutionManagement {
repositoriesMode . set( RepositoriesMode . FAIL_ON_PROJECT_REPOS )
repositories {
.. .
maven { url " https://jitpack.io " }
}
}
build.gradle
(モジュールレベル) ファイルに依存関係を追加します。
dependencies {
implementation ' com.github.dangiashish:Google-Direction-Api:1.6 '
}
public void getRoutePoints ( LatLng start , LatLng end ) {
if ( start == null || end == null ) {
Toast . makeText ( this , "Unable to get location" , Toast . LENGTH_LONG ). show ();
Log . e ( "TAG" , " latlngs are null" );
} else {
RouteDrawing routeDrawing = new RouteDrawing . Builder ()
. context ( MainActivity . this ) // pass your activity or fragment's context
. travelMode ( AbstractRouting . TravelMode . DRIVING )
. withListener ( this ). alternativeRoutes ( true )
. waypoints ( userLoc , destLoc )
. build ();
routeDrawing . execute ();
}
}
Activity/Fragment
にRouteListener
インターフェイス クラスを実装して、操作をオーバーライドします。
@ Override
public void onRouteFailure ( ErrorHandling e ) {
Log . w ( "TAG" , "onRoutingFailure: " + e );
}
@ Override
public void onRouteStart () {
Log . d ( "TAG" , "yes started" );
}
@ Override
public void onRouteSuccess ( ArrayList < RouteInfoModel > routeInfoModelArrayList , int routeIndexing ) {
if ( polylines != null ) {
polylines . clear ();
}
PolylineOptions polylineOptions = new PolylineOptions ();
ArrayList < Polyline > polylines = new ArrayList <>();
for ( int i = 0 ; i < routeInfoModelArrayList . size (); i ++) {
if ( i == routeIndexing ) {
Log . e ( "TAG" , "onRoutingSuccess: routeIndexing" + routeIndexing );
polylineOptions . color ( Color . BLACK );
polylineOptions . width ( 12 );
polylineOptions . addAll ( routeInfoModelArrayList . get ( routeIndexing ). getPoints ());
polylineOptions . startCap ( new RoundCap ());
polylineOptions . endCap ( new RoundCap ());
Polyline polyline = map . addPolyline ( polylineOptions );
polylines . add ( polyline );
}
}
}
@ Override
public void onRouteCancelled () {
Log . d ( "TAG" , "route canceled" )
// restart your route drawing
}
MIT License
Copyright (c) 2023 Ashish Dangi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.