Deepu
Deepu

Reputation: 2616

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. For this I create HttpGet object with http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false url.

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

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 -

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

Upvotes: 10

Views: 31837

Answers (4)

Haseeb Jadoon
Haseeb Jadoon

Reputation: 450

var request = { 
            origin: "33.661565,73.041330",
            destination: "33.662502,73.044061",
            travelMode: google.maps.TravelMode.DRIVING
};

This works fine

Upvotes: 3

Babasaheb
Babasaheb

Reputation: 703

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

Upvotes: 16

geocodezip
geocodezip

Reputation: 161334

The documentation for the v3 API says a google.maps.LatLng or a string. For geographic locations, create and pass in a 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.

Upvotes: 2

Andrew Leach
Andrew Leach

Reputation: 12973

Link to web service documentation

Just use figures for latitude,longitude separated with a comma: for example 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

Upvotes: 8

Related Questions