ITeye博客

android google地图定位开发,且可以自由移动位置重新获取定位,地址信息

hack_zhang · 2013-05-20 15:00 · 手机android开发 · 地图开发 · 移动开发 · Markdown

一:申请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代码  收藏代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <FrameLayout
  7. android:id="@+id/map_layout"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="vertical" >
  11. <com.google.android.maps.MapView
  12. android:id="@+id/map_view"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:apiKey="0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q"
  16. android:clickable="true"
  17. android:enabled="true" />
  18. <LinearLayout
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="center"
  22. android:orientation="vertical"
  23. android:paddingBottom="105dip" >
  24. <TextView
  25. android:id="@+id/map_bubbleText"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:background="@drawable/location_tips"
  29. android:gravity="left|center"
  30. android:maxEms="12"
  31. android:paddingLeft="12dip"
  32. android:paddingRight="10dip"
  33. android:text="@string/load_tips"
  34. android:textColor="#cfcfcf"
  35. android:textSize="14sp" />
  36. </LinearLayout>
  37. <LinearLayout
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_gravity="center"
  41. android:orientation="vertical" >
  42. <ImageView
  43. android:id="@+id/point_image"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="center"
  47. android:layout_marginBottom="30dip"
  48. android:src="@drawable/point_start" />
  49. </LinearLayout>
  50. </FrameLayout>
  51. </LinearLayout>

 

 

三:创建MyLocationManager类主要用于管理经纬度获取方法实现

 

Java代码  收藏代码

  1. package com.android.map;
  2. import android.content.Context;
  3. import android.location.Location;
  4. import android.location.LocationListener;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. public class MyLocationManager {
  9. private final String TAG = "FzLocationManager";
  10. private static Context mContext;
  11. private LocationManager gpsLocationManager;
  12. private LocationManager networkLocationManager;
  13. private static final int MINTIME = 2000;
  14. private static final int MININSTANCE = 2;
  15. private static MyLocationManager instance;
  16. private Location lastLocation = null;
  17. private static LocationCallBack mCallback;
  18. public static void init(Context c , LocationCallBack callback) {
  19. mContext = c;
  20. mCallback = callback;
  21. }
  22. private MyLocationManager() {
  23. // Gps 定位
  24. gpsLocationManager = (LocationManager) mContext
  25. .getSystemService(Context.LOCATION_SERVICE);
  26. Location gpsLocation = gpsLocationManager
  27. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  28. gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  29. MINTIME, MININSTANCE, locationListener);
  30. // 基站定位
  31. networkLocationManager = (LocationManager) mContext
  32. .getSystemService(Context.LOCATION_SERVICE);
  33. Location networkLocation = gpsLocationManager
  34. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  35. networkLocationManager.requestLocationUpdates(
  36. LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,
  37. locationListener);
  38. }
  39. public static MyLocationManager getInstance() {
  40. if (null == instance) {
  41. instance = new MyLocationManager();
  42. }
  43. return instance;
  44. }
  45. private void updateLocation(Location location) {
  46. lastLocation = location;
  47. mCallback.onCurrentLocation(location);
  48. }
  49. private final LocationListener locationListener = new LocationListener() {
  50. public void onStatusChanged(String provider, int status, Bundle extras) {
  51. }
  52. public void onProviderEnabled(String provider) {
  53. }
  54. public void onProviderDisabled(String provider) {
  55. }
  56. public void onLocationChanged(Location location) {
  57. Log.d(TAG, "onLocationChanged");
  58. updateLocation(location);
  59. }
  60. };
  61. public Location getMyLocation() {
  62. return lastLocation;
  63. }
  64. private static int ENOUGH_LONG = 1000 * 60;
  65. public interface LocationCallBack{
  66. /**
  67. * 当前位置
  68. * @param location
  69. */
  70. void onCurrentLocation(Location location);
  71. }
  72. public void destoryLocationManager(){
  73. Log.d(TAG, "destoryLocationManager");
  74. gpsLocationManager.removeUpdates(locationListener);
  75. networkLocationManager.removeUpdates(locationListener);
  76. }
  77. }

   

 

四:创建MyMapOverlay抽象类,并继承Overlay,创建抽象方法 

changePoint(GeoPoint newPoint,int type)用于回调重新获取到的GeoPoint 重新定位地图,并获取地址信息

 

 

Java代码  收藏代码

  1. import android.view.MotionEvent;
  2. import com.google.android.maps.GeoPoint;
  3. import com.google.android.maps.MapView;
  4. import com.google.android.maps.Overlay;
  5. //覆盖整个地图捕捉触控事件的OverLay
  6. public abstract class MyMapOverlay extends Overlay{
  7. private int point_X;
  8. private int point_Y;
  9. private GeoPoint newPoint;
  10. public MyMapOverlay(int x,int y){
  11. point_X = x;
  12. point_Y = y;
  13. }
  14. boolean flagMove=false;
  15. //触控屏幕移动地图,重新根据屏幕中心点获取该点经纬度
  16. @Override
  17. public boolean onTouchEvent(MotionEvent event, MapView mapView) {
  18. System.out.println("X->"+event.getX()+":"+point_X);
  19. System.out.println("Y->"+event.getY()+":"+point_Y);
  20. if(event.getAction() == MotionEvent.ACTION_DOWN){
  21. changePoint(newPoint,1);
  22. }else if(event.getAction() == MotionEvent.ACTION_UP){
  23. newPoint = mapView.getProjection().fromPixels(point_X,point_Y);
  24. changePoint(newPoint,2);
  25. }
  26. return false;
  27. }
  28. public abstract void changePoint(GeoPoint newPoint,int type);
  29. }

 

 

五:MyMapActivity 继承MapActivity类并实现经纬度获取回调接口LocationCallBack 。项目实现如下:

 

Java代码  收藏代码

  1. package com.android.googlemap;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import java.util.Locale;
  5. import android.graphics.Rect;
  6. import android.location.Address;
  7. import android.location.Geocoder;
  8. import android.location.Location;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.view.View;
  13. import android.view.Window;
  14. import android.widget.TextView;
  15. import com.android.map.MyLocationManager;
  16. import com.android.map.MyLocationManager.LocationCallBack;
  17. import com.android.map.MyMapOverlay;
  18. import com.google.android.maps.GeoPoint;
  19. import com.google.android.maps.MapActivity;
  20. import com.google.android.maps.MapController;
  21. import com.google.android.maps.MapView;
  22. import com.google.android.maps.Overlay;
  23. public class MyMapActivity extends MapActivity implements LocationCallBack {
  24. private MapView mapView;
  25. private MapController mMapCtrl;
  26. private MyLocationManager myLocation;
  27. private List<Overlay> mapOverlays;
  28. public GeoPoint locPoint;
  29. private MyMapOverlay mOverlay;
  30. private TextView desText;
  31. private String lost_tips;
  32. private int point_X;
  33. private int point_Y;
  34. private int statusBarHeight;
  35. public final int MSG_VIEW_LONGPRESS = 10001;
  36. public final int MSG_VIEW_ADDRESSNAME = 10002;
  37. public final int MSG_GONE_ADDRESSNAME = 10003;
  38. @Override
  39. public void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. requestWindowFeature(Window.FEATURE_NO_TITLE);
  42. setContentView(R.layout.main);
  43. mapView = (MapView) findViewById(R.id.map_view);
  44. desText = (TextView) this.findViewById(R.id.map_bubbleText);
  45. lost_tips = getResources().getString(R.string.load_tips);
  46. mapView.setBuiltInZoomControls(true);
  47. mapView.setClickable(true);
  48. mMapCtrl = mapView.getController();
  49. point_X = this.getWindowManager().getDefaultDisplay().getWidth() / 2;
  50. point_Y = this.getWindowManager().getDefaultDisplay().getHeight() / 2;
  51. mOverlay = new MyMapOverlay(point_X, point_Y) {
  52. @Override
  53. public void changePoint(GeoPoint newPoint, int type) {
  54. if (type == 1) {
  55. mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME);
  56. } else {
  57. locPoint = newPoint;
  58. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  59. }
  60. }
  61. };
  62. mapOverlays = mapView.getOverlays();
  63. if (mapOverlays.size() > 0) {
  64. mapOverlays.clear();
  65. }
  66. mapOverlays.add(mOverlay);
  67. mMapCtrl.setZoom(12);
  68. MyLocationManager.init(MyMapActivity.this.getApplicationContext(),
  69. MyMapActivity.this);
  70. myLocation = MyLocationManager.getInstance();
  71. }
  72. @Override
  73. protected void onResume() {
  74. super.onResume();
  75. }
  76. @Override
  77. protected void onPause() {
  78. super.onPause();
  79. }
  80. @Override
  81. protected boolean isRouteDisplayed() {
  82. // TODO Auto-generated method stub
  83. return false;
  84. }
  85. public void onCurrentLocation(Location location) {
  86. if (locPoint == null) {
  87. locPoint = new GeoPoint((int) (location.getLatitude() * 1E6),
  88. (int) (location.getLongitude() * 1E6));
  89. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  90. }
  91. }
  92. public void changePoint(GeoPoint locPoint) {
  93. }
  94. /**
  95. * 通过经纬度获取地址
  96. *
  97. * @param point
  98. * @return
  99. */
  100. private String getLocationAddress(GeoPoint point) {
  101. String add = "";
  102. Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
  103. try {
  104. List<Address> addresses = geoCoder.getFromLocation(
  105. point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,
  106. 1);
  107. Address address = addresses.get(0);
  108. int maxLine = address.getMaxAddressLineIndex();
  109. if (maxLine >= 2) {
  110. add = address.getAddressLine(1) + address.getAddressLine(2);
  111. } else {
  112. add = address.getAddressLine(1);
  113. }
  114. } catch (IOException e) {
  115. add = "";
  116. e.printStackTrace();
  117. }
  118. return add;
  119. }
  120. /**
  121. *
  122. * 用线程异步获取
  123. */
  124. Runnable getAddressName = new Runnable() {
  125. public void run() {
  126. String addressName = "";
  127. while (true) {
  128. addressName = getLocationAddress(locPoint);
  129. if (!"".equals(addressName)) {
  130. break;
  131. }
  132. }
  133. Message msg = new Message();
  134. msg.what = MSG_VIEW_ADDRESSNAME;
  135. msg.obj = addressName;
  136. mHandler.sendMessage(msg);
  137. }
  138. };
  139. private Handler mHandler = new Handler() {
  140. @Override
  141. public void handleMessage(Message msg) {
  142. switch (msg.what) {
  143. case MSG_VIEW_LONGPRESS:// 处理长按时间返回位置信息
  144. {
  145. if (null == locPoint)
  146. return;
  147. new Thread(getAddressName).start();
  148. desText.setVisibility(View.VISIBLE);
  149. desText.setText(lost_tips);
  150. mMapCtrl.animateTo(locPoint);
  151. mapView.invalidate();
  152. }
  153. break;
  154. case MSG_VIEW_ADDRESSNAME:
  155. desText.setText((String) msg.obj);
  156. desText.setVisibility(View.VISIBLE);
  157. if (statusBarHeight == 0) {
  158. Rect frame = new Rect();
  159. getWindow().getDecorView().getWindowVisibleDisplayFrame(
  160. frame);
  161. statusBarHeight = frame.top;
  162. point_Y -= statusBarHeight / 2;
  163. }
  164. break;
  165. case MSG_GONE_ADDRESSNAME:
  166. desText.setVisibility(View.GONE);
  167. break;
  168. }
  169. }
  170. };
  171. // 关闭程序也关闭定位
  172. @Override
  173. protected void onDestroy() {
  174. // TODO Auto-generated method stub
  175. super.onDestroy();
  176. myLocation.destoryLocationManager();
  177. }
  178. }

   

六:在AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限已经google地图库

 

Xml代码  收藏代码

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  5. <!--在application里面添加google地图库如下(一定要记得添加):-->
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <uses-library android:name="com.google.android.maps" />
  8. <activity android:name="com.android.googlemap.MyMapActivity"  android:screenOrientation="portrait"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. </application>

 

 

七:运行效果:

1

2以后后重新获取如下:
 

 

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。