android google地图定位开发,且可以自由移动位置重新获取定位,地址信息
一:申请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代码- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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类主要用于管理经纬度获取方法实现
Java代码- packagecom.android.map;
- importandroid.content.Context;
- importandroid.location.Location;
- importandroid.location.LocationListener;
- importandroid.location.LocationManager;
- importandroid.os.Bundle;
- importandroid.util.Log;
- publicclassMyLocationManager{
- privatefinalStringTAG="FzLocationManager";
- privatestaticContextmContext;
- privateLocationManagergpsLocationManager;
- privateLocationManagernetworkLocationManager;
- privatestaticfinalintMINTIME=2000;
- privatestaticfinalintMININSTANCE=2;
- privatestaticMyLocationManagerinstance;
- privateLocationlastLocation=null;
- privatestaticLocationCallBackmCallback;
- publicstaticvoidinit(Contextc,LocationCallBackcallback){
- mContext=c;
- mCallback=callback;
- }
- privateMyLocationManager(){
- //Gps定位
- gpsLocationManager=(LocationManager)mContext
- .getSystemService(Context.LOCATION_SERVICE);
- LocationgpsLocation=gpsLocationManager
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
- MINTIME,MININSTANCE,locationListener);
- //基站定位
- networkLocationManager=(LocationManager)mContext
- .getSystemService(Context.LOCATION_SERVICE);
- LocationnetworkLocation=gpsLocationManager
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- networkLocationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,MINTIME,MININSTANCE,
- locationListener);
- }
- publicstaticMyLocationManagergetInstance(){
- if(null==instance){
- instance=newMyLocationManager();
- }
- returninstance;
- }
- privatevoidupdateLocation(Locationlocation){
- lastLocation=location;
- mCallback.onCurrentLocation(location);
- }
- privatefinalLocationListenerlocationListener=newLocationListener(){
- publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){
- }
- publicvoidonProviderEnabled(Stringprovider){
- }
- publicvoidonProviderDisabled(Stringprovider){
- }
- publicvoidonLocationChanged(Locationlocation){
- Log.d(TAG,"onLocationChanged");
- updateLocation(location);
- }
- };
- publicLocationgetMyLocation(){
- returnlastLocation;
- }
- privatestaticintENOUGH_LONG=1000*60;
- publicinterfaceLocationCallBack{
- /**
- *当前位置
- *@paramlocation
- */
- voidonCurrentLocation(Locationlocation);
- }
- publicvoiddestoryLocationManager(){
- Log.d(TAG,"destoryLocationManager");
- gpsLocationManager.removeUpdates(locationListener);
- networkLocationManager.removeUpdates(locationListener);
- }
- }
四:创建MyMapOverlay抽象类,并继承Overlay,创建抽象方法
changePoint(GeoPointnewPoint,inttype)用于回调重新获取到的GeoPoint重新定位地图,并获取地址信息
Java代码- importandroid.view.MotionEvent;
- importcom.google.android.maps.GeoPoint;
- importcom.google.android.maps.MapView;
- importcom.google.android.maps.Overlay;
- //覆盖整个地图捕捉触控事件的OverLay
- publicabstractclassMyMapOverlayextendsOverlay{
- privateintpoint_X;
- privateintpoint_Y;
- privateGeoPointnewPoint;
- publicMyMapOverlay(intx,inty){
- point_X=x;
- point_Y=y;
- }
- booleanflagMove=false;
- //触控屏幕移动地图,重新根据屏幕中心点获取该点经纬度
- @Override
- publicbooleanonTouchEvent(MotionEventevent,MapViewmapView){
- 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);
- }elseif(event.getAction()==MotionEvent.ACTION_UP){
- newPoint=mapView.getProjection().fromPixels(point_X,point_Y);
- changePoint(newPoint,2);
- }
- returnfalse;
- }
- publicabstractvoidchangePoint(GeoPointnewPoint,inttype);
- }
五:MyMapActivity继承MapActivity类并实现经纬度获取回调接口LocationCallBack。项目实现如下:
Java代码- packagecom.android.googlemap;
- importjava.io.IOException;
- importjava.util.List;
- importjava.util.Locale;
- importandroid.graphics.Rect;
- importandroid.location.Address;
- importandroid.location.Geocoder;
- importandroid.location.Location;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.view.View;
- importandroid.view.Window;
- importandroid.widget.TextView;
- importcom.android.map.MyLocationManager;
- importcom.android.map.MyLocationManager.LocationCallBack;
- importcom.android.map.MyMapOverlay;
- importcom.google.android.maps.GeoPoint;
- importcom.google.android.maps.MapActivity;
- importcom.google.android.maps.MapController;
- importcom.google.android.maps.MapView;
- importcom.google.android.maps.Overlay;
- publicclassMyMapActivityextendsMapActivityimplementsLocationCallBack{
- privateMapViewmapView;
- privateMapControllermMapCtrl;
- privateMyLocationManagermyLocation;
- privateList<Overlay>mapOverlays;
- publicGeoPointlocPoint;
- privateMyMapOverlaymOverlay;
- privateTextViewdesText;
- privateStringlost_tips;
- privateintpoint_X;
- privateintpoint_Y;
- privateintstatusBarHeight;
- publicfinalintMSG_VIEW_LONGPRESS=10001;
- publicfinalintMSG_VIEW_ADDRESSNAME=10002;
- publicfinalintMSG_GONE_ADDRESSNAME=10003;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- 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=newMyMapOverlay(point_X,point_Y){
- @Override
- publicvoidchangePoint(GeoPointnewPoint,inttype){
- 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
- protectedvoidonResume(){
- super.onResume();
- }
- @Override
- protectedvoidonPause(){
- super.onPause();
- }
- @Override
- protectedbooleanisRouteDisplayed(){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- publicvoidonCurrentLocation(Locationlocation){
- if(locPoint==null){
- locPoint=newGeoPoint((int)(location.getLatitude()*1E6),
- (int)(location.getLongitude()*1E6));
- mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
- }
- }
- publicvoidchangePoint(GeoPointlocPoint){
- }
- /**
- *通过经纬度获取地址
- *
- *@parampoint
- *@return
- */
- privateStringgetLocationAddress(GeoPointpoint){
- Stringadd="";
- GeocodergeoCoder=newGeocoder(getBaseContext(),Locale.getDefault());
- try{
- List<Address>addresses=geoCoder.getFromLocation(
- point.getLatitudeE6()/1E6,point.getLongitudeE6()/1E6,
- 1);
- Addressaddress=addresses.get(0);
- intmaxLine=address.getMaxAddressLineIndex();
- if(maxLine>=2){
- add=address.getAddressLine(1)+address.getAddressLine(2);
- }else{
- add=address.getAddressLine(1);
- }
- }catch(IOExceptione){
- add="";
- e.printStackTrace();
- }
- returnadd;
- }
- /**
- *
- *用线程异步获取
- */
- RunnablegetAddressName=newRunnable(){
- publicvoidrun(){
- StringaddressName="";
- while(true){
- addressName=getLocationAddress(locPoint);
- if(!"".equals(addressName)){
- break;
- }
- }
- Messagemsg=newMessage();
- msg.what=MSG_VIEW_ADDRESSNAME;
- msg.obj=addressName;
- mHandler.sendMessage(msg);
- }
- };
- privateHandlermHandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){
- caseMSG_VIEW_LONGPRESS://处理长按时间返回位置信息
- {
- if(null==locPoint)
- return;
- newThread(getAddressName).start();
- desText.setVisibility(View.VISIBLE);
- desText.setText(lost_tips);
- mMapCtrl.animateTo(locPoint);
- mapView.invalidate();
- }
- break;
- caseMSG_VIEW_ADDRESSNAME:
- desText.setText((String)msg.obj);
- desText.setVisibility(View.VISIBLE);
- if(statusBarHeight==0){
- Rectframe=newRect();
- getWindow().getDecorView().getWindowVisibleDisplayFrame(
- frame);
- statusBarHeight=frame.top;
- point_Y-=statusBarHeight/2;
- }
- break;
- caseMSG_GONE_ADDRESSNAME:
- desText.setVisibility(View.GONE);
- break;
- }
- }
- };
- //关闭程序也关闭定位
- @Override
- protectedvoidonDestroy(){
- //TODOAuto-generatedmethodstub
- super.onDestroy();
- myLocation.destoryLocationManager();
- }
- }
六:在AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限已经google地图库
Xml代码- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
- <!--在application里面添加google地图库如下(一定要记得添加):-->
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <uses-libraryandroid:name="com.google.android.maps"/>
- <activityandroid:name="com.android.googlemap.MyMapActivity"android:screenOrientation="portrait"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
七:运行效果:
1
2以后后重新获取如下:
提示:如果没有出现地图只出现地址信息背景是网格的,那就是key的问题,根据开始的方法找到自己开发的eclipse中sdk的debugkeystore位置重新申请key即可
更多相关文章
- Android(安卓)呼吸灯流程分析(一)
- 巧解Android时区加载过慢的问题
- Android(安卓)Native C development (2) -- framebuffer
- [置顶] Android(安卓)《手机卫士》随听笔记
- android studio 获取证书指纹 SHA1
- Android(安卓)开发常用代码
- Android(安卓)Fragment的增加,删除,添加
- Android基础笔记(五)-网络编程
- android与javascript交互调用