简体   繁体   English

如何将纬度 - 经度传递给Google Maps JavaScript API v3路线服务?

[英]How to pass latitude-longitude to Google Maps JavaScript API v3 Directions service?

I want to use Google Maps JavaScript API v3 in my Java Application. 我想在我的Java应用程序中使用Google Maps JavaScript API v3 For this I create HttpGet object with http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false url. 为此,我使用http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false网址创建了HttpGet对象。

I get the proper response, but instead of passing Station name I want to pass the latitude-longitude of the stations. 我得到了正确的答案,但我没有通过电台名称 ,而是希望通过电台纬度 - 经度

A documentation can be found at Here 可以在Here找到文档

How can I pass latitude-longitude to this service? 如何将纬度 - 经度传递给此服务?

EDIT : 编辑:

When I give the URL as - http://maps.googleapis.com/maps/api/directions/json?origin=Jackson+Av&destination=Prospect+Av&sensor=false I get the proper response, but when I give URL as - 当我将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 I get the response as - ZERO RESULT http://maps.googleapis.com/maps/api/directions/json?origin=40.81649,73.907807&destination=40.819585,-73.90177&sensor=false我收到的回复是 - 零结果

You can create url to get as follow, 您可以创建网址,如下所示,

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);

Please check you are providing correct geoco-ordinates 请检查您是否提供正确的地理坐标

Link to web service documentation 链接到Web服务文档

Just use figures for latitude,longitude separated with a comma: for example 51,0 . 只需使用数字表示纬度,经度用逗号分隔:例如51,0 Make sure there are no spaces. 确保没有空格。

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

The documentation for the v3 API says a google.maps.LatLng or a string. v3 API文档说明了google.maps.LatLng或字符串。 For geographic locations, create and pass in a google.maps.LatLng; 对于地理位置,请创建并传入google.maps.LatLng; for addresses, pass in a string. 对于地址,传入一个字符串。

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

And in the reference 并在参考

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.

and for a waypoint : 并为航点

location    LatLng|string   Waypoint location. Can be an address string or LatLng. Optional.
var request = { 
            origin: "33.661565,73.041330",
            destination: "33.662502,73.044061",
            travelMode: google.maps.TravelMode.DRIVING
};

This works fine 这很好用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM