9

我想在我的 Java 应用程序中使用Google Maps JavaScript API v3 。为此,我使用http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false url创建HttpGet对象。

我得到了正确的响应,但是我不想传递站名,而是想传递站的经纬度

可以在此处找到文档

如何将经纬度传递给此服务?

编辑 :

当我将 URL 提供为 - http://maps.googleapis.com/maps/api/directions/json?origin=Jackson+Av&destination=Prospect+Av&sensor=false 我得到正确的响应,但是当我将 URL 提供为 -

http://maps.googleapis.com/maps/api/directions/json?origin=40.81649,73.907807&destination=40.819585,-73.90177&sensor=false 我得到的响应是 -零结果

4

4 回答 4

15

您可以创建 url 得到如下,

double lat1 = 40.74560;
   double lon1 = -73.94622000000001;
   double lat2 = 46.59122000000001;
   double lon2 = -112.004230;

   String url = "http://maps.googleapis.com/maps/api/directions/json?";

   List<NameValuePair> params = new LinkedList<NameValuePair>();
   params.add(new BasicNameValuePair("origin", lat1 + "," + lon1));
   params.add(new BasicNameValuePair("destination", lat2 + "," + lon2));
   params.add(new BasicNameValuePair("sensor", "false"));

   String paramString = URLEncodedUtils.format(params, "utf-8");
   url += paramString;
   HttpGet get = new HttpGet(url);

请检查您提供的地理坐标是否正确

于 2012-12-08T06:46:09.913 回答
8

链接到 Web 服务文档

只需使用数字表示纬度,经度用逗号分隔:例如51,0. 确保没有空格。

http://maps.googleapis.com/maps/api/directions/json?origin=51,0&destination=51.5,-0.1&sensor=false

于 2012-12-07T13:17:28.430 回答
3
var request = { 
            origin: "33.661565,73.041330",
            destination: "33.662502,73.044061",
            travelMode: google.maps.TravelMode.DRIVING
};

这工作正常

于 2014-12-01T17:56:51.107 回答
2

v3 API的文档说google.maps.LatLng或字符串。对于地理位置,创建并传入 google.maps.LatLng;对于地址,传入一个字符串。

origin:      LatLng | String,
destination: LatLng | String,

并且在参考

destination LatLng|string   Location of destination. This can be specified as either a string to be geocoded or a LatLng. Required.
origin      LatLng|string   Location of origin. This can be specified as either a string to be geocoded or a LatLng. Required.

和一个航点

location    LatLng|string   Waypoint location. Can be an address string or LatLng. Optional.
于 2012-12-07T13:55:47.687 回答