0

I am trying to create an app that will launch a Google Maps Intent that will show the route to point A, passing through points B and C. I have the addresses and the LatLng of theses points.

Until now I tried to use just make a route from one pont to another using what is in this answer but it didn't work. When the Google Maps app opens it says that no route was found. The origin and destination fields are filled with the latitude and longitude of my points.

What am I doing wrong? Is there another way of doing this?

EDIT: Code I'm using to start the intent:

double ori_latitude = -90.0000000;
double ori_longitude = -60.0000000;
double dest_latitude = -90.0000000;
double dest_longitude = -54.0000000;

String uri = String.format(Locale.ENGLISH, "http://maps.google.com/dir ?saddr=%f,%f&daddr=%f,%f", ori_latitude, ori_longitude, dest_latitude, dest_longitude);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");

startActivity(intent);
Community
  • 1
  • 1
fzanutto
  • 13
  • 3
  • You'll be more likely to get help on this question if you post a [MCVE](http://stackoverflow.com/help/mcve) – Jason Braucht Dec 22 '16 at 16:46
  • You have a space in the `uri` – OneCricketeer Dec 22 '16 at 17:05
  • @cricket_007, removed the space and nothing changed. Error keeps happening – fzanutto Dec 22 '16 at 17:16
  • Even if I enter that filled out URL in the browser, it doesn't work. http://maps.google.com/maps?saddr=-90.000000,-60.000000&daddr=-90.000000,-54.000000 – OneCricketeer Dec 22 '16 at 17:27
  • Also, the url is `/maps?` not `/dir?`. The linked post you used has no `/dir?` – OneCricketeer Dec 22 '16 at 17:28
  • @cricket_007 I saw on some site a url with /dir? and tested with it. Forgot to change back when posted the code here. Neither of them worked. – fzanutto Dec 22 '16 at 17:44
  • Well, just entering your (-90, -60) as a coordinate into google maps doesn't point anywhere special and even if that was flipped (-60,-90) is in the middle of the southern Pacific – OneCricketeer Dec 22 '16 at 17:47
  • 1
    Oh, i guess i realized what is wrong. As you said the coordinates are from nowhere. I got them from the geocoded addresses, i guess they failed and I just realized this now. – fzanutto Dec 22 '16 at 17:54

2 Answers2

0

Code you have written is correct. There is issue with these coordinates. Try changing coordinates, and see the magic. It works perfectly. For eg:

double ori_latitude =  34.052222;
double ori_longitude = -118.243611;
double dest_latitude = 37.322778;
double dest_longitude = -122.031944;

Also, check the rules at Google Maps

Tips for formatting your coordinates
Here are some tips for formatting your coordinates so they work on Google Maps:
Use the degree symbol instead of "d".
Use periods as decimals, not commas.
Incorrect: 41,40338, 2,17403. Correct: 41.40338, 2.17403.
List your latitude coordinates before longitude coordinates.
Check that the first number in your latitude coordinate is between -90 and 90.
Check that the first number in your longitude coordinate is between -180 and 180.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hitesh Jain
  • 408
  • 2
  • 5
0

Just a quick glance says your code would work, but your coordinates point nowhere special, so I wouldn't be surprised if no route existed.

From a quick test, this works for me.

Seattle, WA to San Francisco, CA

double[] origin = { 47.605617, -122.332137 };
double[] dest = { 37.774821, -122.419462 };

Uri.Builder builder = new Uri.Builder()
    .scheme("http")
    .authority("maps.google.com/maps")
    .appendQueryParameter("saddr", String.format(Locale.ENGLISH, "%f,%f", origin[0], origin[1]))
    .appendQueryParameter("daddr", String.format(Locale.ENGLISH, "%f,%f", dest[0], dest[1]));

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(builder.build().toString()));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");

startActivity(intent);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245