user1784207
user1784207

Reputation: 13

Trying to Launch Google Maps using URI and Intents

I'm trying to make a button in my app open the google maps app at a specific location. I have been trying to use a URI and intent. When I try to start the activity, the app quits unexpectedly. This only happens when I'm using geo:x.x,x.x. I'm able to make it open a web address, but not the google maps app. I'm not really sure what the problem is here, but here is the onclick method for the button I'm using:

  mapButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v){

            String UriString = getResources().getString(R.string.map_location);
            Uri geoUri = Uri.parse(UriString);
            Intent mapCall = new Intent(Intent.ACTION_VIEW, geoUri);
            startActivity(mapCall);
        }

   });

I feel like this should be simple enough, but it just won't work for me. Any help would be appreciated!

In case it helps, this is the rest of the code I've written so far:

public class MainActivity extends Activity implements OnClickListener{

public Button courseButton;
public Button mapButton;
public Button newsButton;
public Button directoryButton;
public Button calendarButton;
public Button videoButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapButton = (Button)findViewById(R.id.mapButton);
    courseButton = (Button)findViewById(R.id.courseButton);
    newsButton = (Button)findViewById(R.id.newsButton);
    directoryButton = (Button)findViewById(R.id.directoryButton);
    calendarButton = (Button)findViewById(R.id.calendarButton);
    videoButton = (Button)findViewById(R.id.videoButton);

    courseButton.setOnClickListener(this);

    mapButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v){


            String UriString = getResources().getString(R.string.map_location);
            Uri geoUri = Uri.parse(UriString);
            Intent mapCall = new Intent(Intent.ACTION_VIEW, geoUri);

            startActivity(mapCall);
            }


    });


    newsButton.setOnClickListener(new OnClickListener(){

        public void onClick(View v){
            String UriString = getResources().getString(R.string.rss_feed);
            Uri rssUri = Uri.parse(UriString);
            Intent rssCall = new Intent(Intent.ACTION_VIEW, rssUri);
            startActivity(rssCall);
        }
    });


    directoryButton.setOnClickListener(this);
    calendarButton.setOnClickListener(this);
    videoButton.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub

    }   
}

I'm just really at a loss here.

As requested:

10-29 23:02:25.031: E/AndroidRuntime(324): FATAL EXCEPTION: main
10-29 23:02:25.031: E/AndroidRuntime(324): android.content.ActivityNotFoundException: No    Activity found to handle Intent { act=android.intent.action.VIEW  dat=geo:44.873799766954136, -91.92715644836426?z=22 }
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.app.Activity.startActivityForResult(Activity.java:2827)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.app.Activity.startActivity(Activity.java:2933)
10-29 23:02:25.031: E/AndroidRuntime(324):  at com.example.stout.MainActivity$1.onClick(MainActivity.java:46)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.view.View.performClick(View.java:2485)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.view.View$PerformClick.run(View.java:9080)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.os.Handler.handleCallback(Handler.java:587)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.os.Looper.loop(Looper.java:123)
10-29 23:02:25.031: E/AndroidRuntime(324):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-29 23:02:25.031: E/AndroidRuntime(324):  at java.lang.reflect.Method.invokeNative(Native Method)
10-29 23:02:25.031: E/AndroidRuntime(324):  at java.lang.reflect.Method.invoke(Method.java:507)
10-29 23:02:25.031: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-29 23:02:25.031: E/AndroidRuntime(324):  at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-29 23:02:25.031: E/AndroidRuntime(324):  at dalvik.system.NativeStart.main(Native     Method)

Upvotes: 1

Views: 1967

Answers (3)

Andres MR
Andres MR

Reputation: 21

For me works fine:

Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + object.getLatitude() + "," + object.getLongitude() + "(" + object.getName() + ")");

Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);

mapIntent.setPackage("com.google.android.apps.maps");

startActivity(mapIntent);

Upvotes: 1

Pete
Pete

Reputation: 595

You are seeing the activity not found exception because google maps is not installed on the default emulator/ is not part of AOSP builds which the emulator uses.

Upvotes: 0

Joe Malin
Joe Malin

Reputation: 8641

Well, the stack trace says that Android can't find an app that handles ACTION_VIEW and the data geo: 44.873799766954136, -91.92715644836426?z=22

Unfortunately, the documentation says that the feature is "under development" and not fully supported. There's no guarantee that it's been implemented the way that the documentation says.

Upvotes: 0

Related Questions