0

I am facing a very strange problem while implementing Google navigation in my Android application.

I am implementing Google navigation by opening the URL -

https://maps.google.co.in/maps?saddr=xxxxxxxxxxxxxxxxxx&daddr=xxxxxxxxxxxxx

where xxxx means source address and destination address.

Using an implicit intent. I am successfully able to get the navigation to the place by opening the web url in desktop.

But when i try to run the application on the device, the url redirects itself to google search page. And when i press back button, i get a navigation route from current location (as per google map), to current location obtained using Reverse Geocoding. In short the url above mentioned does't serve its purpose.

Please help me solve the problem. If the Google navigation implementation is wrong, please help me out in that as well.

Thanks in advance

Utkarsh Mankad
  • 247
  • 1
  • 6
  • 18

3 Answers3

8
Intent intent = new Intent( Intent.ACTION_VIEW, 
            Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
            "&saddr=31.249351,121.45905&daddr=31.186371,121.489885&hl=zh&t=m&dirflg=d")); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    startActivity(intent);

you can use the code to start the google map app to navigate! ps: dirflg= d; you can change the parameter "d" means driving car, "w" means walking "r" means by bus or others.

perry
  • 856
  • 1
  • 10
  • 22
3

An ideal approach to access maps from android will be to fire an intent from an activity with the required details of source and destination locations instead of trying to call the map url like in desktop. The intent will roughly ike:

Intent navigation = new Intent(Intent.ACTION_VIEW, Uri
        .parse("http://maps.google.com/maps?saddr="
                + Constants.latitude + ","
                + Constants.longitude + "&daddr="
                + latitude + "," + longitude));
startActivity(navigation);

This earlier post has more discussion on this approach. Please check if it suits your requirement

Community
  • 1
  • 1
tony m
  • 4,769
  • 1
  • 21
  • 28
0
            Intent navigation = new Intent(Intent.ACTION_VIEW, Uri
                    .parse("http://maps.google.com/maps?saddr="
                            + latitude  + ","
                            + longitude +
                            "&daddr="
                            + lat + "," + long1));
           navigation.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
           navigation.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
            startActivity(navigation);
Poonam
  • 21
  • 1
  • 1
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Michael Dodd Oct 11 '17 at 15:38