第一步:申请com.google.android.maps.MapView apiKey 获取debug keystore的位置:Eclipse——>Windows——>Perferences——>Android——>Build查看Default debug keystor,比如我的C:/Users/Administrator/.android/debug.keystore 在找到在cmd中运行keytool -list -alias androiddebugkey -keystore "C:/Users/Administrator/.android/debug.keystore" -storepass android -keypass android,路径上一定要加双引号 执行结果: androiddebugkey, 2010-3-16, PrivateKeyEntry, 认证指纹 (MD5): XX:XX:XX:E3:XX:D5:XX:43:DF:5D:F0:97:XX:XX:5E:9E 这里就是你的认证指纹 打开打开http://code.google.com/intl/zh-CN/android/maps-api-signup.html,填写你刚刚生成的认证指纹就能获取apiKey了,这里需要登录Google Account,如果没有先申请一个. 我获取的结果是: 第二步:一切准备就绪,只欠东风了. 新建Android项目,Target 选择Google APIs,或者更改已有项目的Target为Google APIs,打开AndroidManifest.xml文件, 加入和 我的是:
将MapView加入界面,如main.xml
第三步:编写Activity,将你的Activtity继承自MapActivity,如: package com.lhw.android; import android.os.Bundle; import com.google.android.maps.MapActivity; public class MainActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected boolean isRouteDisplayed() { return false; } } 第四步:想要运行这个工程,还需要新建一个Google APIs AVD,这个在Eclipse里新建很简单,也可以用命令建(我当时在这用的以前的Android AVD导致地图显示不出来,呵呵) 第二个例子:实现Zoom功能 在main.xml中加入: 在MainActivity.java代码: public class MainActivity extends MapActivity { private MapView mapView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView=(MapView)this.findViewById(R.id.mapView); LinearLayout zoomLayout=(LinearLayout)this.findViewById(R.id.zoom); View zoomView=mapView.getZoomControls(); zoomLayout.addView(zoomView); mapView.displayZoomControls(true); } @Override protected boolean isRouteDisplayed() { return false; } } Run,效果如图: 功能扩展:按下键盘↑,↓键实现Zoom功能,在onCreate方法中增加 mapView.setStreetView(true);或mapView.setSatellite(true)自定义显示 @Override public boolean onKeyDown(int keyCode,KeyEvent event){ MapController mc=mapView.getController(); switch(keyCode){ case KeyEvent.KEYCODE_DPAD_UP: mc.zoomIn(); break; case KeyEvent.KEYCODE_DPAD_DOWN: mc.zoomOut(); break; } return super.onKeyDown(keyCode, event); } 第三个例子:在屏幕上打点(因为在现在正在做的GIS项目中叫的“打点”,哈哈): 1)自动打点 package com.lhw.android; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; public class MainActivity extends MapActivity { private MapView mapView; private MapController mc; private GeoPoint p; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView=(MapView)this.findViewById(R.id.mapView); mapView.setSatellite(true); LinearLayout zoomLayout=(LinearLayout)this.findViewById(R.id.zoom); View zoomView=mapView.getZoomControls(); zoomLayout.addView(zoomView,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); mapView.displayZoomControls(true); mc=mapView.getController(); String coordinates[]={"1.352566007", "103.78921587"}; double latitude=Double.parseDouble(coordinates[0]); double longitude=Double.parseDouble(coordinates[1]); p=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); mc.animateTo(p); mc.setZoom(17); mapView.invalidate(); } @Override public boolean onKeyDown(int keyCode,KeyEvent event){ switch(keyCode){ case KeyEvent.KEYCODE_DPAD_UP: mc.zoomIn(); break; case KeyEvent.KEYCODE_DPAD_DOWN: mc.zoomOut(); break; } return super.onKeyDown(keyCode, event); } @Override protected boolean isRouteDisplayed() { return false; } } 2)在打点出显示图标 package com.lhw.android; import java.util.List; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Point; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; public class MainActivity extends MapActivity { private MapView mapView; private MapController mc; private GeoPoint p; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView=(MapView)this.findViewById(R.id.mapView); mapView.setStreetView(true); LinearLayout zoomLayout=(LinearLayout)this.findViewById(R.id.zoom); View zoomView=mapView.getZoomControls(); zoomLayout.addView(zoomView,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); mapView.displayZoomControls(true); mc=mapView.getController(); String coordinates[]={"1.352566007", "103.78921587"}; double latitude=Double.parseDouble(coordinates[0]); double longitude=Double.parseDouble(coordinates[1]); p=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6)); mc.animateTo(p); mc.setZoom(17); //加入新的图层(我这样理解的) MapOverlay mapOverlay=new MapOverlay(); List listOfOverlays=mapView.getOverlays(); listOfOverlays.clear(); listOfOverlays.add(mapOverlay); mapView.invalidate(); } @Override public boolean onKeyDown(int keyCode,KeyEvent event){ switch(keyCode){ case KeyEvent.KEYCODE_DPAD_UP: mc.zoomIn(); break; case KeyEvent.KEYCODE_DPAD_DOWN: mc.zoomOut(); break; } return super.onKeyDown(keyCode, event); } @Override protected boolean isRouteDisplayed() { return false; } //新加代码,创建新图层类 class MapOverlay extends Overlay{ @Override public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when){ super.draw(canvas, mapView, shadow); Point screenPts=new Point(); mapView.getProjection().toPixels(p, screenPts); Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.mark); canvas.drawBitmap(bmp,screenPts.x,screenPts.y-30,null); return true; } } } 3)显示打点坐标 在MapOverlay类中增加以下方法 @Override public boolean onTouchEvent(MotionEvent event,MapView mapView){ if(event.getAction()==1){ GeoPoint p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY()); Toast.makeText(getBaseContext(),p.getLatitudeE6()/1E6+","+p.getLongitudeE6()/1E6, Toast.LENGTH_SHORT).show(); } return false; } 4)显示坐标所在地 修改MapOver类的onTouchEvent方法: @Override public boolean onTouchEvent(MotionEvent event,MapView mapView){ if(event.getAction()==1){ GeoPoint p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY()); Geocoder geoCoder=new Geocoder(getBaseContext(),Locale.getDefault()); try{ List
addresses=geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6,1); String add=""; if(addresses.size()>0){ for(int i=0;i addresses = geoCoder.getFromLocationName( "empire state building", 5); String add = ""; if (addresses.size() > 0) { p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); mc.animateTo(p); mapView.invalidate(); } } catch (IOException e) { e.printStackTrace(); } return true; }else{ return false; } } 在AndroidManifest.xml需加入要素: <application android:icon="@drawable/icon" android:label="@string/app_name"> //..