`

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

阅读更多

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

 

四:创建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.       
  29.     public abstract void changePoint(GeoPoint newPoint,int type);  
  30. }  
 

 

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

 

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

六: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即可

分享到:
评论

相关推荐

    Google Android SDK开发范例大全(第3版) 4/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    Google Android SDK开发范例大全(第3版) 3/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    Google Android SDK开发范例大全(第3版) 5/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    安卓源码包 Android GPS 开发 地图&导航&定位&指南等设计代码合集 (45个).zip

    百度地图移动获取位置,自动定位.zip 百度快速定位locSDK_3.3_Demo.zip 监控别人的行踪.rar 级联菜单,两级菜单自定义实现提供多种方式PopWindow,Fragment引用..rar 自动判断位置的弹出菜单.zip 菜单动画(类似QQ...

    Google Android SDK开发范例大全(第3版) 1/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    Android获取位置信息的方法

    本文实例为大家分享了Android获取位置信息的具体代码,供大家参考,具体内容如下 1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位、移动...

    新版Android开发教程.rar

    将会支持 Google 可能发布的手机操作系统或者应用软件,共同开发名为 Android 的开放源代码的移动 系 统。 谷歌早在 2002 年就进入了移动领域,可是由于目前的手机操作系统企业和手机企业相对封闭,提高了 行业的...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    Android 一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导开发 IOS 由苹果公司开发的移动操作系统 Webkit 一个开源的浏览器引擎,在手机上的...

    android开发实例大全_王东华

    本书以Android应用程序的开发为主题,并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程。本书的讲述由浅入深,实例全面并典型,几乎囊括了所有和Android应用相关的项目。全书分为...

    Android 定位系统(GPS)开发详解

    该地理定位服务可以用来获取当前设备的地理位置,应用程序可以定时请求更新设备当前的地理位置信息。比如应用程序可以借助一个Intent接收器来实现如下功能: 以经纬度和半径规划一个区域,当设备进入给区域,发出...

    Android 传感器数据采集 - 位置传感器 (GPS) 示例:从运行 MATLAB 的计算机中获取移动设备的位置数据并将其可视化。-matlab开发

    此示例展示了如何获取 Android 移动设备(例如手机或平板电脑)的位置和速度数据。 位置数据由纬度、经度和高度数据组成。 在此示例中,捕获的纬度和经度数据在 MATLAB 中绘制,并使用 Mapping Toolbox 覆盖在来自 ...

    集成目前Android主流优秀第三方组件

    这是一个集成目前Android主流优秀第三方组件、优秀好用的自定义控件、实用工具类封装、以及一些APP共通模块(比如:版本更新、意见反馈、引导界面等等)的开发包,帮助程序员快速开发自己的APP 已集成第三方开源...

    UCMAP 开发使用手册

    8, GPS 定位,支持获取手机GPS 定位数据,实现GPS 定位监控,以及基站定位; 9, Bing Maps API,支持访问互联网地图服务Bing Maps; 10,Google Maps API,支持访问互联网地图服务Google Maps; 11,数据融合,...

    Android集成主流优秀第三方组件框架

    地图定位工具类(ToolLocation.java),读取GPS状态、请求定位、获取经纬度等方法 社会化分享工具类(ToolShareSDK.java),各大开发平台分享API操作 短信验证码工具类(ToolMSM.java),移动/联通/电信三网发送手机...

    Android例子源码集成安卓主流优秀第三方组件框架.zip

    这是一个集成目前Android主流优秀第三方组件、优秀好用的自定义控件、实用工具类封装、以及一些APP共通模块(比如:版本更新、意见反馈、引导界面等等)的开发包,帮助程序员快速开发自己的APP 已集成第三方开源...

    android取得所在位置的经纬度

    见博客:http://blog.csdn.net/baidu_nod/article/details/37697685

    基于java的本地旅游天气实时平台

    详细来讲,本文所研讨的移动旅行信息管理系统首要包含三个子系统:景点查询,该子系统能供给对旅行景点方位查询,在地图上标识经纬度值,能对当时及未来三天进行天气预报,能查询两地址间的行走搭车途径以及周边住宿、...

    mylife:Android应用程序通过设备传感器记录用户活动

    My-Life是专为Android平台设计的移动应用程序。 使用不同的智能手机传感器,该软件可以记录白天或锻炼期间用户活动的数据。 收集的信息显示为一组统计信息,并且还以图形或地图的形式显示。 My-Life是由弗罗茨瓦夫...

Global site tag (gtag.js) - Google Analytics