编写android对google地图的调用

来源:互联网 发布:淘宝视频在线制作 编辑:程序博客网 时间:2024/06/05 16:21

编写android对google地图的调用

android可以通过google map api的addon做地图的显示,但是功能不够全面。比如,无法给出到目的地的路线建议等。

要调用全功能的google地图,在android中,可以:

  • 调用android中的google地图(如果有的话);
  • 调用google的web版本地图。

写了个简单的示例,第一页:

image

按上面的按钮,将定位到火车站(这个例子不能得到行车路线)。

默认情况下,将出现选择页面,供用户选择是通过本地的google地图,还是使用web版本的。

image

 

如果使用谷歌地图,则:

image

如果使用浏览器:

image

代码很简单,给按钮的监听器里面加:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(
                Intent.ACTION_VIEW,
                Uri
                        .parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=31.249351,121.45905"));
        startActivity(i);
    }
});

 

这样做的缺点是,跳出了自己应用程序的Activity,到google地图(比如)的Activity了,无法在之上设定自己的Overlay,或者增加控制返回自己应用程序的按钮。

使用google地图,界面等更友好一些,比如menu按钮。浏览器的按钮偏小,按着不方便。

也可以强制使用谷歌地图打开,需要增加:

Intent i = new Intent(
        Intent.ACTION_VIEW,
        Uri
                .parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=31.249351,121.45905"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
i.setClassName("com.google.android.apps.maps",
        "com.google.android.maps.MapsActivity");
startActivity(i);

 

转http://marshal.easymorse.com/archives/2552

如果是调用其他地图,参照改:

Intent intent = new Intent();
        ComponentName cn = new ComponentName(
       "org.geometerplus.zlibrary.ui.android",
       "org.geometerplus.android.fbreader.PdfFlipActivity");
                    intent.setComponent(cn);  
   
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.setAction(android.content.Intent.ACTION_VIEW);
                    
            intent.setDataAndType(Uri.fromFile(mListtmp.file),mListtmp.file_type);
          startActivity(intent);

 

原创粉丝点击