2017-10-08 58 views
-1

有没有什么办法可以从我的应用程序服务器调用Google Maps Web API,即我的意思是说,从Java代码或从任何服务器端代码而不涉及任何JavaScript?谷歌地图来自服务器的Api调用

因为我们可以从我们的android应用程序中调用这些map api,前提是我们的清单文件包含API KEY。但是如何在我的应用程序服务器上实现这一点,它将控制该android应用程序。

+0

是的,你可以这样做[Google maps web API](https://developers.google.com/maps/web-services/)包含很多服务。你在说什么服务? – imk

+0

我在说我是否可以从我的应用程序服务器调用这些Web服务?考虑它作为地方api –

回答

2

由于我最近使用距离矩阵API,这就是为什么我指的是这个例子。对于首先从您的应用服务器进行的Google API调用,您需要通过Maven将该库添加到项目中。

Maven的

<dependency> 
    <groupId>com.google.maps</groupId> 
    <artifactId>google-maps-services</artifactId> 
    <version>0.2.2</version> 
</dependency> 
<dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-nop</artifactId> 
    <version>1.7.25</version> 
</dependency> 

您还可以添加gradle这个dependencies.Then遵循这个例子以下

LatLng origin, LatLng destination 
GeoApiContext context = new GeoApiContext.Builder() 
    .apiKey(your_google_api_key) 
    .build(); 

//This one is for asynchronous calling style 
DistanceMatrixApi.newRequest(context).origins(origin).destinations(destination) 
       .setCallback(new PendingResult.Callback<DistanceMatrix>() { 
       @Override 
       public void onResult(DistanceMatrix result) { 

       Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
       System.out.println(gson.toJson(result.rows[0].elements[0].duration)); 

       } 

       @Override 
       public void onFailure(Throwable e) { 
       // Handle error. 
       } 
    }); 

请求支持同步调用的风格了。还提供了间歇性故障发生时的自动重试。有关更深入的知识和功能,请看这位官员documentation

+0

感谢您的代码。 –