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

相关问题