现在的位置: 首页 > 综合 > 正文

Android Google map使用

2013年12月09日 ⁄ 综合 ⁄ 共 3147字 ⁄ 字号 评论关闭

 

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; @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; } } 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。

抱歉!评论已关闭.