Android——高德地图简单定位小工具

1.首先需要下载高德的jar包,放在libs目录下

2.LocationUtill.java

 1 import android.content.Context;
 2 
 3 import com.amap.api.location.AMapLocationListener;
 4 import com.amap.api.location.LocationManagerProxy;
 5 import com.amap.api.location.LocationProviderProxy;
 6 
 7 
 8 public class LocationUtil{
 9 
10     //amap
11     private static LocationManagerProxy aMapManager;
12     
13     /**
14      * 开启定位是调用
15      * @param context
16      * @param mAMapLocationListener
17      */
18     public static void startAmap(Context context , AMapLocationListener mAMapLocationListener){
19         aMapManager = LocationManagerProxy.getInstance(context);
20         aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, mAMapLocationListener);
21         
22     }
23     /**
24      * 结束定位是调用
25      * @param mAMapLocationListener 根据需要实现这个Listener
26      */
27     public static  void stopAmap(AMapLocationListener mAMapLocationListener) {
28         if (aMapManager != null) {
29             aMapManager.removeUpdates(mAMapLocationListener);
30             aMapManager.destory();
31         }
32         aMapManager = null;
33     }
34     

使用举例:

 MainActivity.java

  1 package com.example.locationdemo;
  2 
  3 import android.app.Activity;
  4 import android.location.Location;
  5 import android.os.Bundle;
  6 import android.os.Handler;
  7 import android.os.Message;
  8 import android.view.View;
  9 import android.view.View.OnClickListener;
 10 import android.widget.Button;
 11 import android.widget.TextView;
 12 
 13 import com.amap.api.location.AMapLocation;
 14 import com.amap.api.location.AMapLocationListener;
 15 
 16 public class MainActivity extends Activity {
 17 
 18     private TextView mTextView;
 19     private Button amapBtn;
 20 
 21 
 22     public static final int RESPONSE = 0;
 23     private Handler handler = new Handler() {
 24 
 25         @Override
 26         public void handleMessage(Message msg) {
 27             switch (msg.what) {
 28             case RESPONSE:
 29                 String str = (String) msg.obj;
 30                 mTextView.setText("高德定位\n" + str);
 31                 break;
 32 
 33             default:
 34                 break;
 35             }
 36         }
 37 
 38     };
 39 
 40     @Override
 41     protected void onCreate(Bundle savedInstanceState) {
 42         super.onCreate(savedInstanceState);
 43         setContentView(R.layout.activity_main);
 44         mTextView = (TextView) findViewById(R.id.text);
 45         amapBtn = (Button) findViewById(R.id.amap);
 46 
 47         amapBtn.setOnClickListener(new OnClickListener() {
 48 
 49             @Override
 50             public void onClick(View arg0) {
 51                 if (amapBtn.getText().toString().equals("开启高德定位")) {
 52                     LocationUtil.startAmap(MainActivity.this,
 53                             mAMapLocationListener);
 54                     amapBtn.setText("停止高德定位");
 55                 } else {
 56                     LocationUtil.stopAmap(mAMapLocationListener);
 57                     amapBtn.setText("开启高德定位");
 58                 }
 59             }
 60         });
 61 
 62     }
 63 
 64     private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
 65 
 66         @Override
 67         public void onStatusChanged(String provider, int status, Bundle extras) {
 68 
 69         }
 70 
 71         @Override
 72         public void onProviderEnabled(String provider) {
 73 
 74         }
 75 
 76         @Override
 77         public void onProviderDisabled(String provider) {
 78 
 79         }
 80 
 81         @Override
 82         public void onLocationChanged(Location location) {
 83 
 84         }
 85 
 86         @Override
 87         public void onLocationChanged(AMapLocation location) {
 88             if (location != null) {
 89                 String province = null;
 90                 province = location.getProvince();
 91                 if (province != null) {   // 判空后再交给handler,不然可能没有获取到数据,提交过去就不好了!
 92                     Message msg = new Message();
 93                     msg.what = RESPONSE;
 94                     msg.obj = province;
 95                     handler.sendMessage(msg);
 96                 }
 97 
 98                 /*
 99                  * 当然还可以获取更多的信息
100                  *  String str = ("定位成功:(" + geoLng + "," + geoLat +
101                  * ")" + "\n精    度    :" + location.getAccuracy() + "米" +
102                  * "\n定位方式:" + location.getProvider() + "\n定位时间:" + new
103                  * Date(location.getTime()).toLocaleString() + "\n城市编码:" +
104                  * cityCode + "\n位置描述:" + desc + "\n省:" + location.getProvince()
105                  * + "\n市:" + location.getCity() + "\n区(县):" +
106                  * location.getDistrict() + "\n区域编码:" + location .getAdCode());
107                  * mTextView.setText("高德定位\n" + str);
108                  */
109 
110             }
111         }
112     };
113 
114 }

 

posted @ 2015-11-20 16:04  洱海  阅读(799)  评论(0编辑  收藏  举报
.First { margin: 10px 0; font-family: 'Microsoft Yahei'; text-align: left; padding: 6px 20px; color: #fff; background: #55895B; font-size: 20px; border-radius: 4px; clear: both; } .Second { margin: 10px 0; font-family: 'Microsoft Yahei'; padding: 6px 20px; background: #93C8A2; color: white; font-size: 18px; border-radius: 4px; clear: both; } .Third { margin: 10px 0; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0; font-size: 16px; color: black; background: #C6EFD2; border-radius: 4px; clear: both; } .note { margin: 10px 0; padding: 15px 20px 15px 60px; background: #FCFAA9 url('http://images.cnblogs.com/cnblogs_com/libaoheng/305804/o_yellow-pin.png') no-repeat 20px 0; font-size: 15px; font-family: 'Microsoft Yahei'; box-shadow: 0 0 8px #aaa; clear: both; } .demo { text-align: left; padding: 6px 20px; overflow: auto; border-radius: 4px; background: orange; color: #fff; font-size: 16px; clear: both; } .cnblogs_Highlighter { border: solid 1px #ccc; clear: both; } .cnblogs_code { background: #EFFFF4; border: solid 0px #939393; font-size: 14px; clear: both; padding: 10px 20px; } .cnblogs_code pre { font-size: 14px; } .cnblogs_code span { font-family: Courier New; font-size: 14px; }