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("高德定位 " + 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 * ")" + " 精 度 :" + location.getAccuracy() + "米" + 102 * " 定位方式:" + location.getProvider() + " 定位时间:" + new 103 * Date(location.getTime()).toLocaleString() + " 城市编码:" + 104 * cityCode + " 位置描述:" + desc + " 省:" + location.getProvince() 105 * + " 市:" + location.getCity() + " 区(县):" + 106 * location.getDistrict() + " 区域编码:" + location .getAdCode()); 107 * mTextView.setText("高德定位 " + str); 108 */ 109 110 } 111 } 112 }; 113 114 }