一:申请key:

1.首先找到debugkeystore位置:

  打开Eclipse--->Windows--->Preferences--->Android--->Build

一般是这样的路径C:\DocumentsandSettings\Administrator\.android\debug.keystore

2.cmd中执行

keytool-list-aliasandroiddebugkey-keystore"C:\DocumentsandSettings\Administrator\.android\debug.keystore"-storepassandroid-keypassandroid


得到认证指纹(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):到下面的Mycertificate'sMD5fingerprint

4.然后点击GenerateApikey

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(GeoPointnewPoint,inttype)用于回调重新获取到的GeoPoint重新定位地图,并获取地址信息

import android.view.MotionEvent;import com.google.android.maps.GeoPoint;import com.google.android.maps.MapView;import com.google.android.maps.Overlay;//覆盖整个地图捕捉触控事件的OverLaypublic 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;@Overridepublic 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) {@Overridepublic 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();}@Overrideprotected void onResume() {super.onResume();}@Overrideprotected void onPause() {super.onPause();}@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn 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() {@Overridepublic 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;}}};// 关闭程序也关闭定位@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.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


2以后后重新获取如下:


提示:如果没有出现地图只出现地址信息背景是网格的,那就是key的问题,根据开始的方法找到自己开发的eclipse中sdk的debugkeystore位置重新申请key即可

更多相关文章

  1. Android(安卓)各种音量的获取和设置
  2. Android对话框里面的输入值获取不到,空指针异常
  3. 初涉Android蓝牙开发 收藏以备后用
  4. android里面EditTex多行输入及输入置顶问题
  5. android google地图定位开发,且可以自由移动位置重新获取定位,地址
  6. android google地图定位开发,且可以自由移动位置重新获取定位,地址
  7. Android进阶(一)几种网络请求方式详解
  8. android google地图定位开发,且可以自由移动位置重新获取定位,地址
  9. Android(安卓)Layout XML属性研究--android:layout_marginBottom

随机推荐

  1. WCF 实例 —— Android 短信助手 (WCF +
  2. 自定义View之入门(一)
  3. Android Activity生命周期具体内容概述
  4. Android学习笔记:Android异步任务之AsyncT
  5. android:contentDescription 的用途的用
  6. Android 计时器Timer用法
  7. Android 屏幕适配全攻略
  8. Android调试桥-Android(安卓)Debug Birdge
  9. 关于Android横竖屏的设置和使用
  10. Android(安卓)Handler 机制