Goodle MAP使用

4771 ワード


Android Google mapの心得
著者:汪春
Android Google map使用
1、Android Google Map Appを使用する前に、システムに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アプリを使用することができます.使い方は以下の通りです.
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、この方法を使うにはAndroid Manifest.xmlに該当するアクセス権限を追加する必要があります.
<uses-permission android:name="
android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="
android.permission.INTERNET" />;
B、自分のアプリキーを申請したいです.