第一步:建一個包含Google APIs的工程
第二步:獲取Android Maps API 金鑰
第三步:載入MapView
第四步:通過MapController控制地圖縮放與移動
獲取密鑰的方法:
第一步:C:\Users\Jet>keytool -list -alias androiddebugkey -keystore "C:\Users\Jet\.android\debug.keystore" -storepass android -keypass android
紅字部分是keystore的具體路徑,此處修改成自己電腦上keystore的真實路徑即可
執行命令後,會得到MD5認證指紋
第二步:打開http://code.google.com/intl/zh-CN/android/maps-api-signup.html,在頁面最下部有一處輸入認證指紋得到密鑰的地方。將你得到的MD5指紋輸入,提交即可得到密鑰。(上面這個網址貌似需要翻牆之後才能正常打開,否則輸入指紋的地方顯示不出來)
密鑰格式:0QB6Bk20bHSaAJIZN******3upyNFLK_tujWTBw
注意:
1)不要把經度和緯度搞混,否則影響正常顯示
2)在manifest.xml的<application>標簽中加這一句<uses-library android:name="com.google.android.maps"/>
- public class LLAddressActivity extends MapActivity {
- private MapController mMapController;
- private MapView mapView;
- private Button button1, button2, button3;
- private EditText longet, latiet;
- private int zoomLevel = 0;
- private double dLati = 25.0402555;
- private double dLong = 121.512377;
- private String longitude = "";
- private String latitude = "";
-
- @Override
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.lladdress);
-
- //find components
- mapView = (MapView) findViewById(R.id.address_mapview);
- button1 = (Button) findViewById(R.id.address_search);
- button2 = (Button) findViewById(R.id.address_plus);
- button3 = (Button) findViewById(R.id.address_minus);
- longet = (EditText) findViewById(R.id.longtitude_et);
- latiet = (EditText) findViewById(R.id.latitude_et);
-
- mMapController = mapView.getController();
- // 設置MapView的顯示選項
- mapView.setSatellite(true);
-
- //默認的放大層級
- zoomLevel = 17;
- mMapController.setZoom(zoomLevel);
- refreshMapView();
-
- button1.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- longitude = longet.getText().toString();
- latitude = latiet.getText().toString();
- dLong = Double.parseDouble(longitude);
- dLati = Double.parseDouble(latitude);
- refreshMapView();
- Log.e("MAP", "latitude:" + dLati + "logitude" + longitude);
- }
- });
- button2.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- zoomLevel++;
- if (zoomLevel > mapView.getMaxZoomLevel()) {
- zoomLevel = mapView.getMaxZoomLevel();
- }
- mMapController.setZoom(zoomLevel);
- }
- });
- button3.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- zoomLevel--;
- if (zoomLevel < 1) {
- zoomLevel = 1;
- }
- mMapController.setZoom(zoomLevel);
- }
- });
- }
-
- /**
- * 重整Map的Method
- */
- private void refreshMapView() {
- GeoPoint p = new GeoPoint((int) (dLati * 1E6), (int) (dLong * 1E6));
- mapView.displayZoomControls(true);
- /* 將Map的中點移動GeoPoint */
- mMapController.animateTo(p);
- mMapController.setZoom(zoomLevel);
- }
-
- @Override
- protected boolean isRouteDisplayed() {
- return false;
- }
-
- }
布局文件中使用這樣一段來顯示地圖
- <com.google.android.maps.MapView
- android:id="@+id/address_mapview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true"
- android:apiKey="0QB6Bk20b**********upyNFLK_tujWTBw" />