Android中调用外部地图程序

来源:互联网 发布:moto2008概预算软件 编辑:程序博客网 时间:2024/05/16 00:46

http://blog.csdn.net/gf771115/article/details/7722456

http://express.ruanko.com/ruanko-express_26/technologyexchange6.html


一、通过geo-uri方式调用外部程序,可以启动google map,百度地图等:

//geo:latitude,longitude//geo:latitude,longitude?z=zoom,z表示zoom级别,值为数字1到23//geo:0,0?q=my+street+address//geo:0,0?q=business+near+cityUri mUri = Uri.parse("geo:39.940409,116.355257?q=西直门");Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);startActivity(mIntent);

这段代码将会弹出一个对话框,显示所有在initer-filter中注册了geo-uri类型的程序,让用户进行选择,如果我们的程序也需要支持处理geo-uri,可以通过在AndroidMainfest文件中添加如下代码来实现:

<intent-filter android:priority="0" >      <action android:name="android.intent.action.VIEW" />      <category android:name="android.intent.category.DEFAULT" />      <category android:name="android.intent.category.BROWSABLE" />      <data android:scheme="geo" /></intent-filter>

效果预览:

Android调用外部地图程序

Android调用外部地图程序


二、通过谷歌地图的MapsActivity:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=39.940409,116.355257(西直门)"));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);

上面的代码将会直接启动谷歌地图并显示对应的点,注意,如果设备中没有安装谷歌地图,将会出现ActivityNotFoundException,也可以直接通过下面的代码来让用户选择通过谷歌地图(如果设备中安装了的话),或者通过浏览器来在线显示地图:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=39.940409,116.355257(西直门)"));startActivity(i);

效果预览:

Android调用外部地图程序

Android调用外部地图程序



Android Google map使用

1、使用Android Google Map Api之前必须检测系统中是否安装了Google map 应用,检测方法如下:

protected boolean checkGoogleMap(){lean isInstallGMap = false;     List<PackageInfo>packs = getPackageManager().getInstalledPackages(0);for (int i = 0; i < packs.size(); i++) {PackageInfo p = packs.get(i);if (p.versionName == null) { // system packages     continue;}if ("com.google.android.apps.maps".equals(p.packageName)) {    isInstallGMap = true;    break;}}return isInstallGMap;}

2、当检测出系统中没有安装Google map 应用时,可以转向Web版的Google map 来访问,如下:

Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q="+weiduExtra+","+jingduExtra+"")); startActivity(it); 注意:使用此方法需在AndroidManifest.xml中加入网络访问权限<uses-permission android:name="android.permission.INTERNET"></uses-permission>

3、当检测出系统中已经安装Google map 应用时,我们就可以使用Google map api 了,使用方法如下:

1)方法一:

Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"+weiduExtra+","+jingduExtra));startActivity(it);注意:使用此方法需在AndroidManifest.xml中加入相应的访问权限<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />

2)方法二:

可以创建一个MapActivity的子类,将MapView显示于其上即可,可以用MapController来控制显示的坐标、地图模式和视野高度,处理起来非常简单。

Java代码 :

    public class MapTest extends MapActivity { private MapView mapView; private MapController mc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapview); mapView = (MapView) findViewById(R.id.map); mapView.setTraffic(true); mc = mapView.getController(); GeoPoint gp = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000)); //地理坐标 mc.animateTo(gp); mc.setZoom(12); } @Override protected boolean isRouteDisplayed() { return false; } }     public class MapTest extends MapActivity {private MapView mapView;private MapController mc;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.mapview);    mapView = (MapView) findViewById(R.id.map);mapView.setTraffic(true);mc = mapView.getController();GeoPoint gp = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000)); //地理坐标mc.animateTo(gp);mc.setZoom(12);}    @Overrideprotected boolean isRouteDisplayed() {return false;}}mapview.xml内容如下: Xml代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="0mHnPl2NS9XPKx6pKwJriV2Wj-mEHSh71yyX_SQ" /> </RelativeLayout>     <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><com.google.android.maps.MapView android:id="@+id/map"android:layout_width="fill_parent" android:layout_height="fill_parent"android:enabled="true"android:clickable="true"android:apiKey="0mHnPl2NS9XPKx6pKwJriV2Wj-mEHSh71yyX_SQ"/></RelativeLayout> 

注意:

A、使用此方法需在AndroidManifest.xml中加入相应的访问权限。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />;

B、你要申请一个自己的apiKey。


原创粉丝点击