尝试使用URI和意图启动Google地图

时间:2012-10-29 22:43:28

标签: android android-intent maps uri

我正在尝试在我的应用中创建一个按钮,在特定位置打开谷歌地图应用。我一直在尝试使用URI和意图。当我尝试启动活动时,应用程序意外退出。这只发生在我使用geo:x.x,x.x时。我可以让它打开一个网址,但不是谷歌地图应用程序。我不太确定这里的问题是什么,但这里是我正在使用的按钮的onclick方法:

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

   });

我觉得这应该很简单,但它对我不起作用。任何帮助将不胜感激!

如果有帮助,这是我到目前为止编写的其余代码:

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

    }   
}

我真的很茫然。

根据要求:

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)

3 个答案:

答案 0 :(得分:1)

对我来说工作正常:

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

答案 1 :(得分:0)

好吧,堆栈跟踪说Android无法找到处理ACTION_VIEW和数据地理位置的应用程序:44.873799766954136,-91.92715644836426?z = 22

不幸的是,文档说该功能正在“开发中”并且不完全支持。不能保证它的实现方式与文档相同。

答案 2 :(得分:0)

您看到活动未找到异常,因为默认模拟器上未安装Google地图/不是模拟器使用的AOSP版本的一部分。