一:申请key:
1.首先找到debug keystore位置:
打开Eclipse--->Windows--->Preferences--->Android--->Build
一般是这样的路径 C:\Documents and Settings\Administrator\.android\debug.keystore
2.在cmd中执行
keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass android -keypass android
得到认证指纹 (MD5)如:6F:C9:41:48:A5:F3:36:A5:D3:DD:B5:D1:CB:AC:47:88
3.打开申请key页面 http://code.google.com/android/maps-api-signup.html
复制 认证指纹 (MD5):到下面的 My certificate's MD5 fingerprint
4.然后点击 Generate Api key
5.等到apikey:0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q
二:设计main.xml如下:(将申请到得key设置在com.google.android.maps.MapView 中如下所示)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/map_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.google.android.maps.MapView android:id="@+id/map_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q" android:clickable="true" android:enabled="true" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" android:paddingBottom="105dip" > <TextView android:id="@+id/map_bubbleText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/location_tips" android:gravity="left|center" android:maxEms="12" android:paddingLeft="12dip" android:paddingRight="10dip" android:text="@string/load_tips" android:textColor="#cfcfcf" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/point_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="30dip" android:src="@drawable/point_start" /> </LinearLayout> </FrameLayout> </LinearLayout>
三:创建MyLocationManager类主要用于管理经纬度获取方法实现
package com.android.map; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; public class MyLocationManager { private final String TAG = "FzLocationManager"; private static Context mContext; private LocationManager gpsLocationManager; private LocationManager networkLocationManager; private static final int MINTIME = 2000; private static final int MININSTANCE = 2; private static MyLocationManager instance; private Location lastLocation = null; private static LocationCallBack mCallback; public static void init(Context c , LocationCallBack callback) { mContext = c; mCallback = callback; } private MyLocationManager() { // Gps 定位 gpsLocationManager = (LocationManager) mContext .getSystemService(Context.LOCATION_SERVICE); Location gpsLocation = gpsLocationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINTIME, MININSTANCE, locationListener); // 基站定位 networkLocationManager = (LocationManager) mContext .getSystemService(Context.LOCATION_SERVICE); Location networkLocation = gpsLocationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); networkLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE, locationListener); } public static MyLocationManager getInstance() { if (null == instance) { instance = new MyLocationManager(); } return instance; } private void updateLocation(Location location) { lastLocation = location; mCallback.onCurrentLocation(location); } private final LocationListener locationListener = new LocationListener() { public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } public void onLocationChanged(Location location) { Log.d(TAG, "onLocationChanged"); updateLocation(location); } }; public Location getMyLocation() { return lastLocation; } private static int ENOUGH_LONG = 1000 * 60; public interface LocationCallBack{ /** * 当前位置 * @param location */ void onCurrentLocation(Location location); } public void destoryLocationManager(){ Log.d(TAG, "destoryLocationManager"); gpsLocationManager.removeUpdates(locationListener); networkLocationManager.removeUpdates(locationListener); } }
四:创建MyMapOverlay抽象类,并继承Overlay,创建抽象方法
changePoint(GeoPoint newPoint,int type)用于回调重新获取到的GeoPoint 重新定位地图,并获取地址信息
import android.view.MotionEvent; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; //覆盖整个地图捕捉触控事件的OverLay public abstract class MyMapOverlay extends Overlay{ private int point_X; private int point_Y; private GeoPoint newPoint; public MyMapOverlay(int x,int y){ point_X = x; point_Y = y; } boolean flagMove=false; //触控屏幕移动地图,重新根据屏幕中心点获取该点经纬度 @Override public boolean onTouchEvent(MotionEvent event, MapView mapView) { System.out.println("X->"+event.getX()+":"+point_X); System.out.println("Y->"+event.getY()+":"+point_Y); if(event.getAction() == MotionEvent.ACTION_DOWN){ changePoint(newPoint,1); }else if(event.getAction() == MotionEvent.ACTION_UP){ newPoint = mapView.getProjection().fromPixels(point_X,point_Y); changePoint(newPoint,2); } return false; } public abstract void changePoint(GeoPoint newPoint,int type); }
五:MyMapActivity 继承MapActivity类并实现经纬度获取回调接口LocationCallBack 。项目实现如下:
package com.android.googlemap; import java.io.IOException; import java.util.List; import java.util.Locale; import android.graphics.Rect; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.Window; import android.widget.TextView; import com.android.map.MyLocationManager; import com.android.map.MyLocationManager.LocationCallBack; import com.android.map.MyMapOverlay; 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 MyMapActivity extends MapActivity implements LocationCallBack { private MapView mapView; private MapController mMapCtrl; private MyLocationManager myLocation; private List<Overlay> mapOverlays; public GeoPoint locPoint; private MyMapOverlay mOverlay; private TextView desText; private String lost_tips; private int point_X; private int point_Y; private int statusBarHeight; public final int MSG_VIEW_LONGPRESS = 10001; public final int MSG_VIEW_ADDRESSNAME = 10002; public final int MSG_GONE_ADDRESSNAME = 10003; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.map_view); desText = (TextView) this.findViewById(R.id.map_bubbleText); lost_tips = getResources().getString(R.string.load_tips); mapView.setBuiltInZoomControls(true); mapView.setClickable(true); mMapCtrl = mapView.getController(); point_X = this.getWindowManager().getDefaultDisplay().getWidth() / 2; point_Y = this.getWindowManager().getDefaultDisplay().getHeight() / 2; mOverlay = new MyMapOverlay(point_X, point_Y) { @Override public void changePoint(GeoPoint newPoint, int type) { if (type == 1) { mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME); } else { locPoint = newPoint; mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS); } } }; mapOverlays = mapView.getOverlays(); if (mapOverlays.size() > 0) { mapOverlays.clear(); } mapOverlays.add(mOverlay); mMapCtrl.setZoom(12); MyLocationManager.init(MyMapActivity.this.getApplicationContext(), MyMapActivity.this); myLocation = MyLocationManager.getInstance(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } public void onCurrentLocation(Location location) { if (locPoint == null) { locPoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS); } } public void changePoint(GeoPoint locPoint) { } /** * 通过经纬度获取地址 * * @param point * @return */ private String getLocationAddress(GeoPoint point) { String add = ""; Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocation( point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 1); Address address = addresses.get(0); int maxLine = address.getMaxAddressLineIndex(); if (maxLine >= 2) { add = address.getAddressLine(1) + address.getAddressLine(2); } else { add = address.getAddressLine(1); } } catch (IOException e) { add = ""; e.printStackTrace(); } return add; } /** * * 用线程异步获取 */ Runnable getAddressName = new Runnable() { public void run() { String addressName = ""; while (true) { addressName = getLocationAddress(locPoint); if (!"".equals(addressName)) { break; } } Message msg = new Message(); msg.what = MSG_VIEW_ADDRESSNAME; msg.obj = addressName; mHandler.sendMessage(msg); } }; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_VIEW_LONGPRESS:// 处理长按时间返回位置信息 { if (null == locPoint) return; new Thread(getAddressName).start(); desText.setVisibility(View.VISIBLE); desText.setText(lost_tips); mMapCtrl.animateTo(locPoint); mapView.invalidate(); } break; case MSG_VIEW_ADDRESSNAME: desText.setText((String) msg.obj); desText.setVisibility(View.VISIBLE); if (statusBarHeight == 0) { Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame( frame); statusBarHeight = frame.top; point_Y -= statusBarHeight / 2; } break; case MSG_GONE_ADDRESSNAME: desText.setVisibility(View.GONE); break; } } }; // 关闭程序也关闭定位 @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); myLocation.destoryLocationManager(); } }
六:在AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限已经google地图库
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!--在application里面添加google地图库如下(一定要记得添加):--> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps" /> <activity android:name="com.android.googlemap.MyMapActivity" android:screenOrientation="portrait" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
七:运行效果:
1
提示:如果没有出现地图只出现地址信息背景是网格的,那就是key的问题,根据开始的方法找到自己开发的eclipse中sdk的debug keystore位置重新申请key即可