Google Direction Api
1.6
이것은 장치 위치와 같은 두 지점(위치) 사이의 목적지 위치까지의 방향 경로를 제공하는 안드로이드 라이브러리입니다. 이 라이브러리를 사용하려면 Goolge Cloud Console에서 프로젝트를 생성하고 Google Direction Api와 함께 Map SDK를 활성화해야 합니다. 설정이 모두 완료된 후 다음의 간단한 단계에 따라 프로젝트에 코드를 구현하세요.
비디오 데모를 보려면 클릭하세요 또는 구현에 대한 유튜브 튜토리얼 | |
영상 | 비디오 데모 |
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.