0

我已經嘗試了幾天,找不到任何使用DirectionsApi的Java Client Library for Google Maps Services 的例子,我看過很多教程,他們的工作要求響應,但我想用這個庫是因爲它是由Google創建的並且受到社區支持。我在他們的github頁面上看到了Geocoding示例,並看到了圖書館的參考網站,但無法理解如何實現DirectionsApi。 我在Android中使用它,並且地理編碼示例正常工作。如何使用Java客戶端庫谷歌地圖服務的DirectionsApi

+1

圖書館不應直接在Android的代碼周圍的API密鑰[安全問題,使用HTTPS ://maps-apis.googleblog.com/2016/09/making-most-of-google-maps-web-service.html]。請介紹一個代理服務器。並且,使用Maher Nabeel爲DirectionsAPI提供的出色例子。希望這可以幫助。 – BhalchandraSW

+0

github上的自述文件說,這個庫是使用代理服務器的絕佳選擇,那麼我該如何去使用它呢? – ateebahmed

回答

1

這裏是一個簡單的片斷

GeoApiContext context = new GeoApiContext().setApiKey("YOUR_API_KEY"); 

DirectionsApiRequest apiRequest = DirectionsApi.newRequest(context); 
apiRequest.origin(new com.google.maps.model.LatLng(latitude, longitude)); 
apiRequest.destination(new com.google.maps.model.LatLng(latitude, longitude)); 
apiRequest.mode(TravelMode.DRIVING); //set travelling mode 

apiRequest.setCallback(new com.google.maps.PendingResult.Callback<DirectionsResult>() { 
    @Override 
    public void onResult(DirectionsResult result) { 
     DirectionsRoute[] routes = result.routes; 

    } 

    @Override 
    public void onFailure(Throwable e) { 

    } 
}); 

要了解其他選項,請參閱文檔: https://developers.google.com/maps/documentation/directions/intro

+0

謝謝,我用了不同的方法,但你看起來不錯! – ateebahmed

相關問題