高德地图定位

对高德地图的定位进行封装,方便调用

如下是高德官网的指导:http://lbs.amap.com/api/android-sdk/guide/create-map/mylocation

如下是主要的代码  定位使用的是(5.0.0版本后的),主要记录下封装的思路

  1 public class MapLbsLayerImpl implements ILbsLayer {
  2 
  3     private Context mContext;
  4     private MapView mapView; // 地图视图对象
  5     private AMap aMap; // 地图管理对象
  6     private AMapLocationClient mLocationClient; // 申明对象
  7     private AMapLocationClientOption mLocationOption = null; // 声明LocationOption对象
  8     private LocationSource.OnLocationChangedListener mListener;
  9     private CommonLocationChangeListener mLocationChangeListener;
 10 
 11     public MapLbsLayerImpl(Context context) {
 12         this.mContext = context;
 13         mapView = new MapView(mContext);
 14         if (aMap == null) aMap = mapView.getMap();
 15         // 隐藏高德地图右下角缩放的按钮
 16         aMap.getUiSettings().setZoomControlsEnabled(false);
 17         aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
 18             @Override
 19             public boolean onMarkerClick(Marker marker) {
 20                 return true;
 21             }
 22         });
 23 
 24     }
 25 
 26     /**
 27      * 地图控件
 28      *
 29      * @return View
 30      */
 31     @Override
 32     public View getMapView() {
 33         return mapView;
 34     }
 35 
 36     /**
 37      * 开始定位
 38      */
 39     @Override
 40     public void getStartLocationMap() {
 41         // 设置定位监听
 42         aMap.setLocationSource(new LocationSource() {
 43             /**
 44              * 激活定位
 45              * @param onLocationChangedListener OnLocationChangedListener
 46              */
 47             @Override
 48             public void activate(OnLocationChangedListener onLocationChangedListener) {
 49                 mListener = onLocationChangedListener;
 50                 if (mLocationClient == null) {
 51                     // 初始化定位
 52                     mLocationClient = new AMapLocationClient(mContext);
 53                     // 初始化定位参数
 54                     mLocationOption = new AMapLocationClientOption();
 55                     // 设置定位回调监听
 56                     mLocationClient.setLocationListener(locationListener);
 57                     // 设置定位模式
 58                     if (ToastUtils.isOpGPS()) {
 59                         // 设置为高精度定位模式
 60                         mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
 61                     } else {
 62                         // 设置为低精度定位模式
 63                         mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
 64                     }
 65                     mLocationOption.setInterval(6000); // 设置多久定位一次
 66                     // mLocationOption.setOnceLocation(true);
 67                     // 设置定位参数
 68                     mLocationClient.setLocationOption(mLocationOption);
 69                     mLocationClient.startLocation(); // 启动定位
 70                 }
 71             }
 72 
 73             /**
 74              * 停止定位
 75              */
 76             @Override
 77             public void deactivate() {
 78                 mListener = null;
 79                 if (mLocationClient != null) {
 80                     mLocationClient.stopLocation();
 81                     mLocationClient.onDestroy();
 82                 }
 83                 mLocationClient = null;
 84             }
 85         });
 86         // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
 87         aMap.setMyLocationEnabled(true);
 88     }
 89 
 90     /**
 91      * 定位的监听
 92      */
 93     private boolean isFirst = true;
 94     private AMapLocationListener locationListener = new AMapLocationListener() {
 95         @Override
 96         public void onLocationChanged(AMapLocation aMapLocation) {
 97             if (mListener != null && aMapLocation != null) {
 98                 if (aMapLocation.getErrorCode() == 0) {
 99                     Log.d("jiejie", "---------定位成功-------");
100                     LocationInfo info = new LocationInfo(aMapLocation.getLatitude(),
101                             aMapLocation.getLongitude());
102                     info.setKey("0000");
103                     if (isFirst) {
104                         isFirst = false;
105                         moveCameraToPoint(info, 15);
106                         if (mLocationChangeListener != null) {
107                             mLocationChangeListener.onLocation(info);
108                         }
109                     }
110                     if (mLocationChangeListener != null) {
111                         mLocationChangeListener.onLocationChanged(info);
112                     }
113                 } else {
114                     Log.d("jiejie", "定位失败" + aMapLocation.getErrorCode() + " " + aMapLocation.getErrorInfo());
115                 }
116             }
117         }
118     };
119     private Map<String, Marker> markerMap = new HashMap<>(); // 管理地图标记的集合
120 
121     /**
122      * 添加Marker
123      *
124      * @param locationInfo 位置信息
125      * @param bitmap       图片
126      * @param isLoad       是否展示动画
127      */
128     @Override
129     public void addOnUpdateMarker(LocationInfo locationInfo, Bitmap bitmap, boolean isLoad) {
130         Marker storeMarker = markerMap.get(locationInfo.getKey());
131         LatLng latLng = new LatLng(locationInfo.getLatitude(), locationInfo.getLongitude());
132         if (storeMarker != null) {
133             // 如果已经存在则更新
134             storeMarker.setPosition(latLng);
135         } else {
136             // 如果没有则创建
137             MarkerOptions options = new MarkerOptions();
138             BitmapDescriptor des = BitmapDescriptorFactory.fromBitmap(bitmap);
139             options.icon(des);
140             options.anchor(0.5f, 0.5f);
141             options.position(latLng);
142             Marker marker = aMap.addMarker(options);
143             if (isLoad) startGrowAnimation(marker);
144             markerMap.put(locationInfo.getKey(), marker);
145         }
146     }
147 
148     /**
149      * 地上生长得Marker
150      *
151      * @param marker Marker
152      */
153     private void startGrowAnimation(Marker marker) {
154         if (marker != null) {
155             Animation animation = new ScaleAnimation(0, 1, 0, 1);
156             animation.setInterpolator(new LinearInterpolator());
157             // 整个移动所需要的时间
158             animation.setDuration(600);
159             // 设置动画
160             marker.setAnimation(animation);
161             // 开始动画
162             marker.startAnimation();
163         }
164     }
165 
166     /**
167      * 位置改变
168      *
169      * @param changeListener Change
170      */
171     @Override
172     public void setLocationChangeListener(CommonLocationChangeListener changeListener) {
173         this.mLocationChangeListener = changeListener;
174     }
175 
176     /**
177      * 移动地图中心到某个点
178      *
179      * @param locationInfo 地址
180      * @param scale        缩放系数
181      */
182     @Override
183     public void moveCameraToPoint(LocationInfo locationInfo, int scale) {
184         LatLng latLng = new LatLng(locationInfo.getLatitude(), locationInfo.getLongitude());
185         CameraUpdate update = CameraUpdateFactory.newCameraPosition(
186                 new CameraPosition(latLng, scale, 0, 0)
187         );
188         aMap.moveCamera(update);
189     }
190 
191 
192     @Override
193     public void onCreate(Bundle bundle) {
194         mapView.onCreate(bundle);
195     }
196 
197     @Override
198     public void onResume() {
199         mapView.onResume();
200     }
201 
202     @Override
203     public void onPause() {
204         mapView.onPause();
205     }
206 
207     @Override
208     public void onDestroy() {
209         mapView.onDestroy();
210         if (null != mLocationClient) {
211             mLocationClient.onDestroy();
212             mLocationClient = null;
213         }
214     }

这样在UI界面就可以更简便的调用了

    private void initView() {
        // 开始定位
        mLbsLayer.getStartLocationMap();
        // 定位返回的监听
        mLbsLayer.setLocationChangeListener(new ILbsLayer.CommonLocationChangeListener() {
            @Override
            public void onLocationChanged(LocationInfo locationInfo) { // 位置改变
                Log.d("jiejie","--main---------");
            }

            @Override
            public void onLocation(LocationInfo locationInfo) { // 第一次定位
                if (mLocationBit == null || mLocationBit.isRecycled()) {
                    mLocationBit = BitmapFactory.decodeResource(getResources(), R.drawable.navi_map_gps_locked);
                }
                mLbsLayer.addOnUpdateMarker(locationInfo, mLocationBit, false); // 第一次定位添加Marker,该Marker不设置生长动画
            }
        });
    }

 

posted @ 2018-01-15 16:05  王丰蛋哥  阅读(1207)  评论(0编辑  收藏  举报